从字符串中删除分号和括号

Remove semicolon and brackets from string

我有这样的字符串:

{
    Result1 = G;
    "Result2" = "";
    Result3 = "1.03";
    Result4 = 6212753389;
}

我如何得到这样的结果:

    Result1 = G
    Result2 = 
    Result3 = 1.03
    Result4 = 6212753389

注意: 我正在尝试 NSCharacterSet 的所有建议。

请帮助我并提前致谢。

试试这个: Swift:

let str = "{ Result1 = G; \"Result2\" = \"\"; Result3 = \"1.03\"; Result4 = 6212753389; }"

var new = str.replacingOccurrences(of: "{", with: "")
new = new.replacingOccurrences(of: "\"", with: "")
new = new.replacingOccurrences(of: "}", with: "")
new = new.replacingOccurrences(of: ";", with: "")

print(new)

Obj-c:

NSString *str = @"{ Result1 = G; \"Result2\" = \"\"; Result3 = \"1.03\"; Result4 = 6212753389; }";

NSString *new = [str stringByReplacingOccurrencesOfString:@"{" withString:@""];
new = [new stringByReplacingOccurrencesOfString:@"}" withString:@""];
new = [new stringByReplacingOccurrencesOfString:@";" withString:@""];
new = [new stringByReplacingOccurrencesOfString:@"\"" withString:@""];

NSLog(@"%@", new);

添加

\n

换行。

错误的问题,或者更好地说:错误的方法或对你的问题的错误解释。 别担心,没关系,我会解释原因。

这是以下的典型结果:

NSString *str = [myDict description];

NSString *str = [NSString stringWithFormat:@"%@", myDict]; //(which relies on `description` method of `NSDictionary`, that's just an hidden call.

查看文档:

NSString uses a format string whose syntax is similar to that used by other formatter objects. It supports the format characters defined for the ANSI C function printf(), plus %@ for any object (see String Format Specifiers and the IEEE printf specification). If the object responds to descriptionWithLocale: messages, NSString sends such a message to retrieve the text representation. Otherwise, it sends a description message. Localizing String Resources describes how to work with and reorder variable arguments in localized strings. Source

几乎从不依赖 description,这更多是为了调试目的。例如,根据我的经验,Apple 至少更改过一次对象的描述。在 ExternalAccessory.framework 中,您可以获得带有 description 的设备的 MAC 地址(这不是 Apple 的策略),但仅限于某些 iOS 版本,并且一旦发现错误,已将其删除。所以如果你有依赖它的代码,那就倒霉了。

您想要的是 NSDictionary 的自定义 "print"。 你可以做什么:

NSMutableString *str = [[NSMutableString alloc] init];
for (id aKey in myDict)
{ 
    [str appendFormat:@"\t%@ = %@\n", aKey, myDict[aKey]];
}

这会在 NSString 的末尾添加一个额外的“\n”。您可以将其删除,或使用 NSMutableArray:

代替
NSMutableArray *array = [[NSMutableArray alloc] init];
for (id aKey in myDict)
{ 
    NSString *aLine = [NSString stringWithFormat:@"\t%@ = %@", aKey, myDict[aKey]];
    [array addObject:aLine];
}
NSString *str = [array componentsJoinedByString:@"\n"];

旁注:为什么 "Result2" 有引用讨论: Why do some dictionary keys have quotes and others don't when printing from debugger?

Why does string show up in NSDictionary with quotes, but others don't?

extension String {
var pureString: String {
    return self.removeCharsFromString(arr: ["{", "}", ";", "\""])
}

func removeCharsFromString(arr: [String]) -> String {
    var str = self
    for char in arr {
        str = str.replacingOccurrences(of: String(char), with: "")
    }
    return str
}
}

用法

let str = """
{
Result1 = G;
"Result2" = "";
Result3 = "1.03";
Result4 = 6212753389;
}
"""
print(str.pureString)