简体   繁体   中英

How to remove the occurence of " characters from nsstring?

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

The inverted commas are reserved chars in objective c and many other languages. 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.

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. If you want the 108 as a NSNumber, you should convert it from NSString to NSNumber. Use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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