ios Swift:sscanf()的等价物是什么?

7kjnsjlb  于 2023-03-20  发布在  iOS
关注(0)|答案(3)|浏览(152)

在我的iOS应用程序中,我将团队名称另存为

very_complex name (number playes)

为了得到球队的全名,我需要按照这种格式读取/拆分该字符串
%s (%s)
就像我们在C语言中使用'' sscanf()'一样。在Swift中我们怎么做呢?

i1icjdpr

i1icjdpr1#

不幸的是,没有sscanf()的替代品,但方法是使用类Scanner(Apple documentationraywenderlich.com)。
对于您的情况:

let str = "My Team Name (2 players)" // %s (%d %s)
let scanner = Scanner(string: str)
var firstStrVal: NSString?
var dummyStrVal: NSString?
var intVal: Int = 0
var secondStrVal: NSString?

scanner.scanUpTo(" (", into: &firstStrVal)
scanner.scanCharacters(from: CharacterSet.init(charactersIn: "("), into: &dummyStrVal)
scanner.scanInt(&intVal)
scanner.scanUpTo(")", into: &secondStrVal)

print(str)
print(firstStrVal!)
print(intVal)
print(secondStrVal!)
print("\(firstStrVal!) (\(intVal) \(secondStrVal!))")

结果:

My Team Name (2 players)
My Team Name
2
players
My Team Name (2 players)
yacmzcpb

yacmzcpb2#

根据Nicholas Allio的建议更新为当前格式

我相信会有人沿着regex解决方案--但在此之前,您始终可以使用componentsSeparatedByString解析字符串

let str = "very_complex name (42 players)"
var splitString1 = str.components(separatedBy: " (")
var splitString2 = splitString1[1].components(separatedBy: " ")

let teamName = splitString2[0]
let numberOfPlayers = Int(splitString2[0])
zazmityj

zazmityj3#

以下是sscanf的近似等效函数,这取决于www.example.com上的Swift OCL库github.com/eclipse/agileuml(在libraries*.zip中):

static func scan(s : String, fmt : String) -> [String]
{ var result : [String] = []

  var ind : Int = 0 // s upto ind has been consumed
  let slen : Int = s.count

  var i : Int = 0
  while i < fmt.count  
  { let c : String = Ocl.at(str: fmt, ind: i+1) 
    if c == "%" && i < fmt.count - 1  
    { let d : String = Ocl.at(str: fmt, ind: i+2) 
      if d == "s"  
      { var schars : String = ""
        for j in ind...slen  
        { let z : String = Ocl.at(str: s, ind: j+1) 
          if Ocl.iswhitespace(s: z) 
          { break }
          else 
          { schars = schars + z }
        }
        result.append(schars)
        ind = ind + schars.count
        i = i + 1  
      }
      else if d == "d"
      { var ichars : String = ""
        for j in ind...slen  
        { let z : String = Ocl.at(str: s, ind: j+1) 
          if Ocl.isMatch(str: z, pattern: "[0-9]") 
          { ichars = ichars + z }
          else 
          { break }
        }
        result.append(ichars)
        ind = ind + ichars.count
        i = i + 1  
      }
      else if d == "f"
      { var fchars : String = ""
        for j in ind...slen  
        { let z : String = Ocl.at(str: s, ind: j+1) 
          if Ocl.isMatch(str: z, pattern: "[0-9.]") 
          { fchars = fchars + z }
          else 
          { break }
        }
        result.append(fchars)
        ind = ind + fchars.count
        i = i + 1  
      }
    }
    else 
    { if Ocl.at(str: s, ind: ind+1) == c   
      { ind = ind+1 }  
      else 
      { return result }
    }
    i = i + 1
  }
  return result 
}

这是这样使用的:打印(扫描(s:“100#20.5/10”,字体:“%d#%f/%d”))

相关问题