简体   繁体   中英

How to remove double quotes and brackets from NSString

I have string in this from ["658681","655917","655904"] i want to change this string in this form 658681,655917,655904 how it can be changed? below is my code of string

- (IBAction)Searchbtn:(id)sender {

    NSData *data=[NSJSONSerialization dataWithJSONObject:getmessageIDArray options:kNilOptions error:nil];
    _finalIDStr=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"the final ID Str==%@",_finalIDStr);



}

this will do it in swift

var stringwithoutquotes = string1.stringByReplacingOccurrencesOfString("\"", withString: "")
var removebracket1 = stringwithoutquotes.stringByReplacingOccurrencesOfString("[", withString: "")
var removebracket2 = removebracket1.stringByReplacingOccurrencesOfString("]", withString: "")

or you could do the entire thing in one line

var string2 = string.stringByReplacingOccurrencesOfString("\"", withString: "").stringByReplacingOccurrencesOfString("[", withString: "").stringByReplacingOccurrencesOfString("]", withString: "")

Here is another cleaner option in swift

var string = "\"hello[]" // string starts as "hello[]
var badchar: NSCharacterSet = NSCharacterSet(charactersInString: "\"[]")
var cleanedstring: NSString = (string.componentsSeparatedByCharactersInSet(badchar) as NSArray).componentsJoinedByString("")
//cleanedstring prints as "hello"

Swift 3:

let string = "\"hello[]" // string starts as "hello[]
let badchar = CharacterSet(charactersIn: "\"[]")
let cleanedstring = string.components(separatedBy: badchar).joined()
//cleanedstring prints as "hello"

Use following code:

NSCharacterSet *unwantedChars = [NSCharacterSet characterSetWithCharactersInString:@"\"[]"];
NSString *requiredString = [[_finalIDStr componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString: @""];

It's efficient and effective way to remove as many characters from your string in one single line..

Swift 4 (String Array) I wanted to convert String Array into String text to be placed in TextView:

FROM
["horse","cat","dog"]

TO
horse
cat
dog

var stringArray = ["horse","cat","dog"]

var stringArrayCleaned = stringArray.description.replacingOccurrences(of: "\"", with: "").replacingOccurrences(of: ",", with: "\n").replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "").replacingOccurrences(of: " ", with: "")


print(stringArrayCleaned)

你可以试试 :

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

I know this is an old question, but in case someone is still looking for answer.. I achieved this in swift using joined(separator:) see: apple documentation

var stringArray = ["alice","bob","cindy"]
print(stringArray.joined(separator: ","))

// this will print: alice,bob,cindy
NSString *str = ["658681","655917","655904"];
NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:@"\"[]"];
str = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

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