stringByTrimmingCharactersInSet:不删除字符串中间的字符

时间:2022-01-30 17:08:46

I want to remove "#" from my string.

我想从我的字符串中删除#。

I have tried

我有试过

 NSString *abc = [@"A#BCD#D" stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]];

But it still shows the string as "A#BCD#D"

但它仍然将字符串显示为“#BCD#D”

What could be wrong?

可能是错的呢?

6 个解决方案

#1


16  

You could try

你可以试试

NSString *modifiedString = [yourString stringByReplacingOccurrencesOfString:@"#" withString:@""];

#2


55  

stringByTrimmingCharactersInSet removes characters from the beginning and end of your string, not from any place in it

stringByTrimmingCharactersInSet从字符串的开头和结尾删除字符,而不是从字符串中的任何位置删除

For your purpose use stringByReplacingOccurrencesOfString:withString: method as others pointed.

为了您的目的,使用stringbyreplacingocurrencesofstring:和其他人指出的一样,使用stringbyreplacingocurrencesofstring:方法。

#3


14  

I wrote a category of NSString for that:

我写了一个NSString的类别

- (NSString *)stringByReplaceCharacterSet:(NSCharacterSet *)characterset withString:(NSString *)string {
    NSString *result = self;
    NSRange range = [result rangeOfCharacterFromSet:characterset];

    while (range.location != NSNotFound) {
        result = [result stringByReplacingCharactersInRange:range withString:string];
        range = [result rangeOfCharacterFromSet:characterset];
    }
    return result;
}

You can use it like this:

你可以这样使用:

NSCharacterSet *funnyCharset = [NSCharacterSet characterSetWithCharactersInString:@"#"];
NSString *newString = [string stringByReplaceCharacterSet:funnyCharset withString:@""];

#4


10  

I previously had a relatively complicated recursive answer for this (see edit history of this answer if you'd like to see that answer), but then I found a pretty simple one liner: 

我之前有一个相对复杂的递归答案(如果你想看到这个答案,请查看这个答案的编辑历史),但是我发现了一个非常简单的问题:

- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet *)characterSet {
    return [[self componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
}

#5


3  

Refer to the Apple Documentation about: stringByReplacingOccurrencesOfString: method in NSString

请参阅Apple文档,了解NSString中的:stringByReplacingOccurrencesOfString:方法

NSString *str1=[str stringByReplacingOccurrencesOfString:@"#" withString:@""];

Hope this helps.

希望这个有帮助。

#6


2  

Use below

使用下面的

NSString * myString = @"A#BCD#D";
NSString * newString = [myString stringByReplacingOccurrencesOfString:@"#" withString:@""];

#1


16  

You could try

你可以试试

NSString *modifiedString = [yourString stringByReplacingOccurrencesOfString:@"#" withString:@""];

#2


55  

stringByTrimmingCharactersInSet removes characters from the beginning and end of your string, not from any place in it

stringByTrimmingCharactersInSet从字符串的开头和结尾删除字符,而不是从字符串中的任何位置删除

For your purpose use stringByReplacingOccurrencesOfString:withString: method as others pointed.

为了您的目的,使用stringbyreplacingocurrencesofstring:和其他人指出的一样,使用stringbyreplacingocurrencesofstring:方法。

#3


14  

I wrote a category of NSString for that:

我写了一个NSString的类别

- (NSString *)stringByReplaceCharacterSet:(NSCharacterSet *)characterset withString:(NSString *)string {
    NSString *result = self;
    NSRange range = [result rangeOfCharacterFromSet:characterset];

    while (range.location != NSNotFound) {
        result = [result stringByReplacingCharactersInRange:range withString:string];
        range = [result rangeOfCharacterFromSet:characterset];
    }
    return result;
}

You can use it like this:

你可以这样使用:

NSCharacterSet *funnyCharset = [NSCharacterSet characterSetWithCharactersInString:@"#"];
NSString *newString = [string stringByReplaceCharacterSet:funnyCharset withString:@""];

#4


10  

I previously had a relatively complicated recursive answer for this (see edit history of this answer if you'd like to see that answer), but then I found a pretty simple one liner: 

我之前有一个相对复杂的递归答案(如果你想看到这个答案,请查看这个答案的编辑历史),但是我发现了一个非常简单的问题:

- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet *)characterSet {
    return [[self componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
}

#5


3  

Refer to the Apple Documentation about: stringByReplacingOccurrencesOfString: method in NSString

请参阅Apple文档,了解NSString中的:stringByReplacingOccurrencesOfString:方法

NSString *str1=[str stringByReplacingOccurrencesOfString:@"#" withString:@""];

Hope this helps.

希望这个有帮助。

#6


2  

Use below

使用下面的

NSString * myString = @"A#BCD#D";
NSString * newString = [myString stringByReplacingOccurrencesOfString:@"#" withString:@""];