我在["658681","655917","655904"]
中有一个字符串我希望以这种形式更改此字符串658681,655917,655904
如何更改它?下面是我的字符串代码
- (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);
}
答案 0 :(得分:6)
这将在swift
中完成var stringwithoutquotes = string1.stringByReplacingOccurrencesOfString("\"", withString: "")
var removebracket1 = stringwithoutquotes.stringByReplacingOccurrencesOfString("[", withString: "")
var removebracket2 = removebracket1.stringByReplacingOccurrencesOfString("]", withString: "")
或者你可以在一行中完成整个事情
var string2 = string.stringByReplacingOccurrencesOfString("\"", withString: "").stringByReplacingOccurrencesOfString("[", withString: "").stringByReplacingOccurrencesOfString("]", withString: "")
这是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"
斯威夫特3:
let string = "\"hello[]" // string starts as "hello[]
let badchar = CharacterSet(charactersIn: "\"[]")
let cleanedstring = string.components(separatedBy: badchar).joined()
//cleanedstring prints as "hello"
答案 1 :(得分:5)
使用以下代码:
NSCharacterSet *unwantedChars = [NSCharacterSet characterSetWithCharactersInString:@"\"[]"];
NSString *requiredString = [[_finalIDStr componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString: @""];
从一行中删除字符串中的多个字符是一种高效且有效的方法..
答案 2 :(得分:1)
Swift 4(String Array) 我想将String Array转换为String文本放在TextView中:
发件人强>
["马""猫""狗"]
以强>
马
猫
狗
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)
答案 3 :(得分:0)
您可以尝试:
[[[_finalIDStr stringByReplacingOccurrencesOfString:@"\"" withString:@""] stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""];
答案 4 :(得分:-2)
NSString *str = ["658681","655917","655904"];
NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:@"\"[]"];
str = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];