简体   繁体   English

如何删除nsstring中出现的字符?

[英]How to remove the occurence of " characters from nsstring?

我对如何删除不需要的字符(例如{,},<,>,很熟悉{,},<,>,但是我无法找到如何删除NSString中出现的“(倒置逗号)。对此的任何建议都是很大的帮助,非常感谢。

The inverted commas are reserved chars in objective c and many other languages. 反向逗号是objective c和许多其他语言的保留字符。 So you have to escape them. 所以你必须逃避他们。 In objective c (and many others) the special-reserved characters are escaped prepending a back slash \\ to the char. 在目标c(以及许多其他目标)中,保留特殊字符的字符转义为反斜杠\\

So, if you have a string like this: 因此,如果您有这样的字符串:

fsdfads"dsfdsa

which you can instantiate escaping the special char, inverted commas, like this: 您可以实例化转义特殊字符,即反逗号,如下所示:

NSString* s = @"fsdfads\"dsfdsa";

You can remove the inverted commas doing: 您可以执行以下操作删除反向逗号:

[s stringByReplacingOccurrencesOfString:@"\"" withString:@""];

Hope this helps! 希望这可以帮助!

EDITED: 编辑:

You comment that you have a string like @"108" and you want to get the 108. If you want it as a NSString, you should do what I posted. 您评论说,您有一个类似@“ 108”的字符串,并且想要获取108。如果您希望将其作为NSString,则应该执行我发布的操作。 If you want the 108 as a NSNumber, you should convert it from NSString to NSNumber. 如果要将108用作NSNumber,则应将其从NSString转换为NSNumber。 Use NSNumberFormatter : 使用NSNumberFormatter

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"108"];
[f release];   // If you are not using ARC

You can do like this: 您可以这样:

NSString *strText = @"abcdef\"\"xyz";
[strText stringByReplacingOccurrencesOfString:@"\"" withString:@""];

If you are talking about quotation marks, then use the following: 如果您在谈论引号,请使用以下命令:

[yourString stringByReplacingOccurrencesOfString:@"\"" withString:@""];

If you are talking about something else, still try using the escape character(setting \\ before that character) 如果您在谈论其他内容,请仍然尝试使用转义符(在该字符之前设置\\

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM