【问题标题】:How to get the first characters in a string? (Swift 3)如何获取字符串中的第一个字符? (斯威夫特 3)
【发布时间】:2017-03-01 17:14:10
【问题描述】:

我想从以"<ONLINE>""<OFFLINE>" (应该成为我的子字符串)开头的字符串中获取子字符串。当我尝试创建 Range 对象时,我可以使用 startIndex 轻松访问第一个字符,但是如何获取子字符串的右括号的索引,该索引将是完整字符串的第 8 个或第 9 个字符?

更新:

一个简单的例子:

let onlineString:String = "<ONLINE> Message with online tag!"

let substring:String = // Get the "<ONLINE> " part from my string?

let onlineStringWithoutTag:String = onlineString.replaceOccurances(of: substring, with: "")

// What I should get as the result: "Message with online tag!"

所以基本上,问题是:我要为substring 做什么?

【问题讨论】:

  • 你能展示一些你如何创建你的范围对象的代码吗?
  • @MichaelDautermann 刚刚用一个例子更新了我的帖子。

标签: swift string substring


【解决方案1】:
let name = "Ajay"

// Use following line to extract first chracter(In String format)

print(name.characters.first?.description ?? "");

// Output : "A"

【讨论】:

  • 这很棒。谢谢
  • 在 swift 4.2 中它说“字符”已被弃用:请直接使用字符串。
【解决方案2】:

如果你不想使用范围

let onlineString:String = "<ONLINE> Message with online tag!"

let substring:String = onlineString.components(separatedBy: " ")[0]

print(substring) // <ONLINE>

【讨论】:

  • 这意味着“>”永远不能包含在标签之外的字符串中。
  • 你想要索引 0,而不是索引 1。你可以使用 onlineString.components(separatedBy: " ")[0] 来保留 ">"。
  • @MichaelDautermann 谢谢,那行得通!但是您能解释一下为什么不必指定您想要删除的部分以"&lt;" 开头。您只指定了取出的子字符串的结尾。还有,[1] 指的是什么?
  • @MichaelDautermann 没关系,我刚刚明白了。基本上,您只是在插入字符出现的每个点将整个分成几块(放在字符串对象数组中),对吗?而[1] 指的是该数组中的第二个 String 对象。
  • @MichaelDautermann 出于某种原因,我认为他想要尾随文本。编辑以匹配评论
【解决方案3】:

正确的方法是使用如下索引:

let string = "123 456"
let firstCharIndex = string.index(string.startIndex, offsetBy: 1)
let firstChar = string.substring(to: firstCharIndex)
print(firstChar)

【讨论】:

    【解决方案4】:

    此代码为您提供字符串的第一个字符。 Swift 提供了这个返回字符的方法?使用前必须包装好

    let str = "FirstCharacter"
    print(str.first!)
    

    【讨论】:

      【解决方案5】:

      类似于 OOPer 的:

      let string = "<ONLINE>"
      
      let closingTag = CharacterSet(charactersIn: ">")
      if let closingTagIndex = string.rangeOfCharacter(from: closingTag) {
          let mySubstring = string.substring(with: string.startIndex..<closingTagIndex.upperBound)
      }
      

      或者使用正则表达式:

      let string = "<ONLINE>jhkjhkh>"
      
      if let range = string.range(of: "<[A-Z]+>", options: .regularExpression) {
          let mySubstring = string.substring(with: range)
      }
      

      【讨论】:

        【解决方案6】:

        此代码对您的目的有所帮助:

        let myString = "<ONLINE>abc"
        if let rangeOfClosingAngleBracket = myString.range(of: ">") {
            let substring = myString.substring(to: rangeOfClosingAngleBracket.upperBound)
            print(substring) //-><ONLINE>
        }
        

        【讨论】:

          【解决方案7】:

          斯威夫特 4

          let firstCharIndex = oneGivenName.index(oneGivenName.startIndex, offsetBy: 1)
          let firstChar = String(oneGivenName[..<firstCharIndex])
          

          【讨论】:

            【解决方案8】:

            在 Swift 5 中

            let someString = "tackoverflow"
            let firstChar = someString.first?.description ?? ""
            print(firstChar)
            

            【讨论】:

              【解决方案9】:

              Swift 5 扩展

              extension String {
                  var firstCharactor: String? {
                      guard self.count > 0 else {
                          return nil
                      }
                      return String(self.prefix(1))
                  }
              }
              

              【讨论】:

                【解决方案10】:
                let character = MyString.first
                

                这是一种快速从字符串中获取第一个字符的简单方法。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-20
                  • 1970-01-01
                  • 2014-07-30
                  • 2015-08-03
                  • 2019-07-15
                  • 2017-03-24
                  • 1970-01-01
                  相关资源
                  最近更新 更多