如何在swift中获取字符串的一部分?

时间:2014-08-13 02:59:20

标签: swift

我想了解如何在Swift中获取字符串的一部分。我正在寻找Swift等价的Mid $,Right $和Left $函数。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

Left的等价物是substringToIndex

示例:(直接来自this site

let myString = "ABCDEFGHI"
let mySubstring = (myString.substringToIndex(2))
//This grabs the first 2 digits of the string and stops there,
//which returns "AB"

Right的(粗略)等价物是substringFromIndex

示例:(直接来自the same site

let myString = "ABCDEFGHI"
let mySubstring = (myString.substringFromIndex(2))
//This jumps over the first 2 digits of the string and grabs the rest,
//which returns "CDEFGHI"

请参阅http://www.learnswiftonline.com/reference-guides/string-reference-guide-for-swift/

答案 1 :(得分:1)

以下是基于Aristocrates答案的rightString,leftString和midString函数的版本。

var myString = "abcdefghijklmnopqrstuvwxyz987654321"

func leftString(theString: String, charToGet: Int) ->String{

var indexCount = 0
let strLen = countElements(theString)

if charToGet > strLen { indexCount = strLen } else { indexCount = charToGet }
if charToGet < 0 { indexCount = 0 }

let index: String.Index = advance(theString.startIndex, indexCount)
let mySubstring:String = theString.substringToIndex(index)

return mySubstring
}

func rightString(theString: String, charToGet: Int) ->String{

var indexCount = 0
let strLen = countElements(theString)
var charToSkip = strLen - charToGet

if charToSkip > strLen { indexCount = strLen } else { indexCount = charToSkip }
if charToSkip < 0 { indexCount = 0 }

let index: String.Index = advance(theString.startIndex, indexCount)
let mySubstring:String = theString.substringFromIndex(index)

return mySubstring
}

func midString(theString: String, startPos: Int, charToGet: Int) ->String{

let strLen = countElements(theString)
var rightCharCount = strLen - startPos

var mySubstring = rightString(theString, rightCharCount)
 mySubstring = leftString(mySubstring, charToGet)

return mySubstring
}

var myLeftString = leftString(myString, 3)
// returns "abc"

var myRightString = rightString(myString, 5)
// returns "54321"

var myMidString = midString(myString, 3, 5)
// returns "defgh"

答案 2 :(得分:1)

在Swift 4中,Left()和right()的等价物是prefix()和suffix()。缺少Mid()等价物。我期待一个名为infix(position:length)的函数。

答案 3 :(得分:0)

这里的解决方案似乎都过时了。这就是我在Swift 2.0中获取带有多个字符的字符串左右两边的结果:

public extension String
{


    public var length: Int { return self.characters.count }  // Swift 2.0


    public func substringToCharactersCount(count: Int) -> String?
    {
        // Discussion:  Equivalent to "left".  Gets the beginning portion of the string with the specified number of characters.  It is friendly to having a count that exceeds the length of the string, returning the portion that it can.

        // returnString
        var returnString: String?

        // length
        let length = self.length

        if length > 0
        {
            // useCount
            let useCount: Int

            if count > length
            {
                useCount = length
            }
            else
            {
                useCount = count
            }

            if useCount > 0
            {
                // useEndIndex
                let useEndIndex = self.startIndex.advancedBy(useCount)

                returnString = self.substringToIndex(useEndIndex)
            }
        }

        return returnString
    }



    public func substringFromCharactersCount(count: Int) -> String?
    {
        // Discussion:  Equivalent to "right".  Gets the ending portion of the string with the specified number of characters.  It is friendly to having a count that exceeds the length of the string, returning the portion that it can.

        // returnString
        var returnString: String?

        // length
        let length = self.length

        if length > 0
        {
            // useCount
            let useCount: Int

            if count > length
            {
                useCount = length
            }
            else
            {
                useCount = count
            }

            if useCount > 0
            {
                // useStartIndex
                let useStartIndex = self.startIndex.advancedBy(length - useCount)

                returnString = self.substringFromIndex(useStartIndex)
            }
        }

        return returnString
    }



}

答案 4 :(得分:0)

Swift 4,Swift5

现代API具有以下语法:

let str = "Hello world!"
let prefix = String(str.prefix(1))
let suffix = String(str.suffix(1))