去除NSString 中的一些特殊字符

方法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 object represents string of ordered characters (text). An NSCharacterSet object represents set of characters in no particular order. It is often much quicker to determine whether character is member of an NSCharacterSet than an NSString. You can't use an NSCharacterSet object to hold text, at least not in any sensible way, because it does not maintain any order of characters, its use is primarily for determining whether character exists in set of characters.

我们在nsstring的分割,查找等操作中,经常会提供两种函数,参数类型分别为NSString NSCharacterset,有什么不同呢?

NSString 是有序字符串

NSCharacterset是无需字符集合,主要用来判断已知字符串是否包含制定字符集,而不可以用来保存字符串。


转自:http://blog.csdn.net/shencaifeixia1/article/details/8232593


ios中 可以使用stringByTrimmingCharactersInSet函数过滤字符串中的特殊符号

首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号

NSCharacterSet *set [NSCharacterSetcharacterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'""];

由于NSString中有全角符号和半角符号因此有些符号要包括全角和半角的

然后调用stringByTrimmingCharactersInSet

NSString *trimmedString [stringstringByTrimmingCharactersInSet:set];

trimmedString就是过滤后的字符串


转自:http://blog.csdn.net/aiyongyyy/article/details/8269546


NSCharacterSet 去除NSString中的空格

去除 username中的空格,tablenewlinenextline 


代码如下:
NSCharacterSet *whitespace =[NSCharacterSet  whitespaceAndNewlineCharacterSet]; 
NSString username =[mUsernameField stringValue]; 
username =[username  stringByTrimmingCharactersInSet:whitespace]; 

注释:

 
stringByTrimmingCharactersInSet: 
Returns new string made by removing from both ends of the receiver characters contained in given characterset. 

whitespaceAndNewlineCharacterSet 
Returns character set containing only the whitespace charactersspace (U+0020) and tab (U+0009) and the newline and nextlinecharacters (U+000A–U+000D, U+0085). 

另外可以用 whitespaceCharacterSet 替换 whitespaceAndNewlineCharacterSet 区别newlinenextline 
whitespaceCharacterSet 
Returns character set containing only the in-line whitespacecharacters space (U+0020) and tab (U+0009).


NSString *temptext =[messageTextField.texts tringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
NSString *text [temptextstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet ]];

1行是去除2端的空格

2行是去除回车


转自:http://blog.sina.com.cn/s/blog_5421851501014xif.html


这是典型的其他语言中trim方法。我要问的是,如何去掉最左边的空格?又该如何去掉最右边的空格?

NSString的类中没有提供实现这类需求的方法,我们只能手工去添加这些方法。

@interface NSString (TrimmingAdditions)  

(NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet  

(NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet  

@end  

  

@implementation NSString (TrimmingAdditions)  


  (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet  

    NSUInteger location 0;  

    NSUInteger length [self length];  

    unichar charBuffer[length];      

    [self getCharacters:charBuffer];  

    for (location; location length; location++)  

        if (![characterSet characterIsMember:charBuffer[location]])  

            break;  

         

     

      return [self substringWithRange:NSMakeRange(location, length location)];  

 

  

(NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet  

    NSUInteger location 0;  

    NSUInteger length [self length];  

    unichar charBuffer[length];      

    [self getCharacters:charBuffer];    

    for (length; length 0; length--)  

        if (![characterSet characterIsMember:charBuffer[length 1]])  

            break;  

         

     

      return [self substringWithRange:NSMakeRange(location, length location)];  

 


@end  


使用stringByTrimmingRightCharactersInSet方法去掉一个字符串最右边的空格。

NSLog(@"%@",[@"abc  123   stringByTrimmingRightCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);  

 如何去掉一个字符串@“0.012300”最右边的“0“呢?这是典型的float格式化成字符串以后的形式,显示时最好去掉最右边的“0”

解决方法如下:

[@"0.012300" stringByTrimmingRightCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]];  

NSStringtrim字符时,不理解为何不提供分别去掉左右字符的方法,如其他语言的ltrimrtim


转自:http://mikixiyou.iteye.com/blog/1623323


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值