简体   繁体   English

如何从 NSString 中删除双引号和括号

[英]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?我有来自["658681","655917","655904"]的字符串 我想以这种形式更改此字符串658681,655917,655904如何更改它? 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 这将在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 这是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: 斯威夫特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: Swift 4(String Array)我想将String Array转换为String文本放在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我知道这是一个老问题,但万一有人还在寻找答案。我在 swift 中使用joined(separator:)实现了这一点,请参阅: 苹果文档

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:@""];

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

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