方法1
*----------------在串中搜索子串----------------*/
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
/*----------------抽取子串 ----------------*/
//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
//去除(☆_☆)的一个例子
-(NSMutableArray *)removeAndDrawTitle
{
NSMutableArray *arrstrTitle = [[[NSMutableArray alloc]initWithCapacity:2]autorelease];
int index= 0;
for (int k = 0 ; k < [self length]-4; k++) {
NSString *stringtitle;
NSString *s4 = [self substringWithRange:NSMakeRange(k,5 )];
if ([s4 isEqualToString:@"(☆_☆)"]) {
if(k==0)
{
index = index +5;
}
if (k!=0) {
stringtitle = [self substringWithRange:NSMakeRange(index,k-index )];
index = k+5;
[arrstrTitle addObject:stringtitle];
}
}
}
return arrstrTitle;
}
方法二
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement这明显就是一个修改NSString的
NSString *oldStr = @“这个字/符串不/需/要斜/线。。”
NSString *newStr = [oldStr stringByReplacingOccurrencesOfString:@"/" withString:@""];
An
我们在nsstring的分割,查找等操作中,经常会提供两种函数,参数类型分别为NSString
NSString
NSCharacterset是无需字符集合,主要用来判断已知字符串是否包含制定字符集,而不可以用来保存字符串。
转自:http://blog.csdn.net/shencaifeixia1/article/details/8232593
在ios中
首先自己定义一个NSCharacterSet,
NSCharacterSet
由于NSString中有全角符号和半角符号,
然后调用stringByTrimmingCharacte
NSString
trimmedString就是过滤后的字符串
转自:http://blog.csdn.net/aiyongyyy/article/details/8269546
NSCharacterSet
去除
代码如下:
NSCharacterSet
NSString
username
注释:
stringByTrimmingCharacte
Returns
whitespaceAndNewlineChar
Returns
另外可以用
whitespaceCharacterSet
Returns
NSString
NSString
第1行是去除2端的空格
第2行是去除回车
转自:http://blog.sina.com.cn/s/blog_5421851501014xif.html
这是典型的其他语言中trim方法。我要问的是,如何去掉最左边的空格?又该如何去掉最右边的空格?
在NSString的类中没有提供实现这类需求的方法,我们只能手工去添加这些方法。
@interface
-
-
@end
@implementation
}
-
}
@end
使用stringByTrimmingRightCha
NSLog(@"%@",[@"abc
解决方法如下:
[@"0.012300"
在NSString中trim字符时,不理解为何不提供分别去掉左右字符的方法,如其他语言的ltrim和rtim。
转自:http://mikixiyou.iteye.com/blog/1623323