【问题标题】:Delete specific part of String in Objective-C在 Objective-C 中删除字符串的特定部分
【发布时间】:2017-11-09 11:37:56
【问题描述】:

我有一个字符串,它是 文件名 [1234568]。我需要删除字符串的方括号部分并获取文件名。我们怎样才能只检索文件名?

【问题讨论】:

  • 你期望的 OP 是什么
  • 只是文件名
  • 你的意思是你需要Filename1234568Filename 1234568
  • 最好提供样本输入和输出的期望...
  • 示例输入为文件名 [12345678],输出为文件名

标签: ios objective-c nsstring


【解决方案1】:

下面的正则表达式可能会对你有所帮助

NSMutableString *str = [@"Filename [1234568]" mutableCopy];
NSRegularExpression *regex = [NSRegularExpression         
                              regularExpressionWithPattern:@"\\[.+?\\]"
                              options:NSRegularExpressionCaseInsensitive
                              error:NULL];

NSString *newStr = [regex replaceMatchesInString:str 
                      options:0 
                        range:NSMakeRange(0, [str length])  
                 withTemplate:@""];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-03
    • 2020-06-17
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多