删除Swift中String对象中特定字符后所有字符的更优雅方法是什么

时间:2021-10-20 20:23:30

What is the more elegant way to remove all characters after specific character in the String object in Swift?

删除Swift中String对象中特定字符后所有字符的更优雅方法是什么?

Suppose that I have the following string:

假设我有以下字符串:

str.str

str.str

and I need to remove the ".str" from it. How can I do it?

我需要从中删除“.str”。我该怎么做?

Thanks in advance.

提前致谢。

3 个解决方案

#1


25  

Here is a way to do it:

这是一种方法:

var str = "str.str"

if let dotRange = str.rangeOfString(".") {
    str.removeRange(dotRange.startIndex..<str.endIndex)
}

Update In Swift 3 it is:

在Swift 3中更新它是:

var str = "str.str"

if let dotRange = str.range(of: ".") {
  str.removeSubrange(dotRange.lowerBound..<str.endIndex)
}

#2


8  

Quite a compact way would be:

相当紧凑的方式是:

var str = "str.str"
str = str.componentsSeparatedByString(".")[0]

Another option you might be interested in, which works for your example 'str.str' but doesn't fit your specification is:

您可能感兴趣的另一个选项,适用于您的示例'str.str',但不符合您的规范:

str = str.stringByDeletingPathExtension
// Returns a new string made by deleting the extension (if any, and only the last)
// from the `String`

#3


8  

I think this is better approach:

我认为这是更好的方法:

Update with Swift 4: (substring is now deprecated)

使用Swift 4更新:(现在不推荐使用子字符串)

let smth = "element=value"

if let index = (smth.range(of: "=")?.upperBound)
{
  //prints "value"
  let afterEqualsTo = String(smth.suffix(from: index))

  //prints "element="
  let beforeEqualsToContainingSymbol = String(smth.prefix(upTo: index))
}

if let index = (smth.range(of: "=")?.lowerBound)
{
  //prints "=value"
  let afterEqualsToContainingSymbol = String(smth.suffix(from: index))

  //prints "element"
  let beforeEqualsTo = String(smth.prefix(upTo: index))
}

#1


25  

Here is a way to do it:

这是一种方法:

var str = "str.str"

if let dotRange = str.rangeOfString(".") {
    str.removeRange(dotRange.startIndex..<str.endIndex)
}

Update In Swift 3 it is:

在Swift 3中更新它是:

var str = "str.str"

if let dotRange = str.range(of: ".") {
  str.removeSubrange(dotRange.lowerBound..<str.endIndex)
}

#2


8  

Quite a compact way would be:

相当紧凑的方式是:

var str = "str.str"
str = str.componentsSeparatedByString(".")[0]

Another option you might be interested in, which works for your example 'str.str' but doesn't fit your specification is:

您可能感兴趣的另一个选项,适用于您的示例'str.str',但不符合您的规范:

str = str.stringByDeletingPathExtension
// Returns a new string made by deleting the extension (if any, and only the last)
// from the `String`

#3


8  

I think this is better approach:

我认为这是更好的方法:

Update with Swift 4: (substring is now deprecated)

使用Swift 4更新:(现在不推荐使用子字符串)

let smth = "element=value"

if let index = (smth.range(of: "=")?.upperBound)
{
  //prints "value"
  let afterEqualsTo = String(smth.suffix(from: index))

  //prints "element="
  let beforeEqualsToContainingSymbol = String(smth.prefix(upTo: index))
}

if let index = (smth.range(of: "=")?.lowerBound)
{
  //prints "=value"
  let afterEqualsToContainingSymbol = String(smth.suffix(from: index))

  //prints "element"
  let beforeEqualsTo = String(smth.prefix(upTo: index))
}