4

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);



}
Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
Mishal Awan
  • 724
  • 2
  • 11
  • 25
  • Try using `stringByReplacingOccurrencesOfString:` of `NSString` or refer http://stackoverflow.com/questions/713918/replace-multiple-characters-in-a-string-in-objective-c – Akhilrajtr Dec 17 '14 at 07:58
  • Double _colon_ would be `::`, presumably you mean the double quote `"`. – paxdiablo Dec 17 '14 at 08:02
  • http://stackoverflow.com/questions/27520752/how-to-remove-double-quotes-and-brackets-from-nsstring – user3182143 Sep 22 '16 at 12:22

6 Answers6

6

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"
chengsam
  • 7,315
  • 6
  • 30
  • 38
nsij22
  • 869
  • 10
  • 20
6

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..

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
5

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)
mikeT
  • 363
  • 3
  • 11
0

You can try :

[[[_finalIDStr stringByReplacingOccurrencesOfString:@"\"" withString:@""] stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""];
rdurand
  • 7,342
  • 3
  • 39
  • 72
0

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
mgcaguioa
  • 1,443
  • 16
  • 19
-2
NSString *str = ["658681","655917","655904"];
NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:@"\"[]"];
str = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
ugur
  • 824
  • 1
  • 7
  • 15