NSString选择部分去字符串objective-c

时间:2012-07-19 15:08:06

标签: iphone objective-c

我有字符串问题。字符串显示:~00000000:termometr2: +26.9 st.C我想在textfield.text中仅使用此部分:+26.9 st.C

由于

3 个答案:

答案 0 :(得分:3)

NSString *fullStr = @"00000000:termometr2: +26.9 st.C";
NSArray *parts = [fullStr componentsSeparatedByString:@" "];
textField.text =[NSString stringWithFormat:@"%@",[parts objectAtIndex:1]];

答案 1 :(得分:2)

它可能对你有帮助:

NSArray *_array = [yourString componentsSeparatedByString:@":"];
[myTextField setText:[_array lastObject]]; // or any other component you want

答案 2 :(得分:0)

只需这样做,使用方法 stringByReplacingOccurrencesOfString:withString:

NSString *originalString = @"~00000000:termometr2: +26.9 st.C";
NSString *filteredString = [originalString stringByReplacingOccurrencesOfString:@"~00000000:termometr2: " withString:@""];