【问题标题】:Remove semicolon and brackets from string从字符串中删除分号和括号
【发布时间】:2018-08-16 15:43:02
【问题描述】:

我有一个这样的字符串:

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

我如何得到这样的结果:

    Result1 = G
    Result2 = 
    Result3 = 1.03
    Result4 = 6212753389

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

请帮助我并提前感谢。

【问题讨论】:

  • 到目前为止你做了哪些努力?也请分享一下
  • @PPL 等待更新那些解决方案
  • 尝试以编程方式替换字符
  • @Vinodh 如何以编程方式替换字符?
  • 对我来说,这似乎是NSDictionarydescription。为什么你有 descriptionNSDictionary 而不是 NSDictionary 引用/指针?如果你有它,那么迭代它并按照你的意愿打印会容易得多:NSMutableString *str = [[NSMutableString alloc] init];for (id aKey in myDict){[str appendFormat:@"\t%@ = %@\n", aKey, myDict[aKey]];} NSLog(@"%@", str);

标签: ios objective-c nsstring


【解决方案1】:

试试这个: 斯威夫特:

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)

对象-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

换行。

【讨论】:

  • 你能用objective c写这段代码吗?因为我不擅长swift,谢谢回复
  • @RealmOfFire 你在这里 :)
【解决方案2】:

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

这是典型的结果:

NSString *str = [myDict description];

或者

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

查看文档:

NSString 使用一个格式字符串,其语法类似于 其他格式化程序对象。它支持为定义的格式字符 ANSI C 函数 printf(),加上任何对象的 %@(参见字符串 格式说明符和 IEEE printf 规范)。如果对象 响应 descriptionWithLocale: 消息,NSString 发送这样一个 消息以检索文本表示。否则,它会发送一个 描述消息。本地化字符串资源描述了如何工作 在本地化字符串中使用并重新排序变量参数。 Source

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

您想要的是您的NSDictionary 的自定义“打印”。 你可以做什么:

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?

【讨论】:

    【解决方案3】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 2019-10-12
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      相关资源
      最近更新 更多