How to Trim a String in Swift based on a character

swift trim string to length
swift string remove character at index
swift split string into characters
swift string parsing
trim string extension swift
swift string remove suffix
string trim extension swift 4
remove substring from string swift

I would like to trim this string, so that I can extract the filename, which is always preceded by an "_" (underscore). What is the best way to do this?

https://s3.amazonaws.com/brewerydbapi/beer/RXI2cT/upload_FfAPfl-icon.png

I would like the result to be FfAPfl-icon.png

You can use String method rangeOfString:

let link = "https://s3.amazonaws.com/brewerydbapi/beer/RXI2cT/upload_FfAPfl-icon.png"
if let range = link.rangeOfString("_") {
    let fileName = link.substringFromIndex(range.endIndex)
    print(fileName)  // "FfAPfl-icon.png\n"
}

Xcode 8 beta 3 • Swift 3

if let range = link.range(of: "_") {
    let fileName = link.substring(from: range.upperBound)
    print(fileName) // "FfAPfl-icon.png\n"
}

trimmingCharacters(in:), Returns a new string made by removing from both ends of the receiver characters contained in a given character set. SDKs. iOS 2.0+; macOS 10.2+; Mac Catalyst  Strings and Characters¶. A string is a series of characters, such as "hello, world" or "albatross".Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values.

Since it is an URL, you can get to the string via:

Swift version 3

let url = URL(string: "https://s3.amazonaws.com/brewerydbapi/beer/RXI2cT/upload_FfAPfl-icon.png")
let component = url?.lastPathComponent?.characters.split(separator:"_").map(String.init)

print(component?.last)// Optional("FfAPfl-icon.png")

In Swift version 2.2 or 3

let url = NSURL(string: 
let component = url?.lastPathComponent?.characters.split{$0 == "_"}.map(String.init)

print(component?.last)

How to trim whitespace in a string, These characters are often removed or trimmed. In Foundation, we locate a useful method called stringByTrimmingCharactersInSet. A character set is required. A String is now a collection of characters by default so iterating over a String or Substring gives you each character in the `String: // Swift 4 for character in country { print ( character ) } To get the first or last character in a String .

For swift 3.X and swift 4.0

let strUrl = "https://www.youtube.com/watch?v=HUNcbm9sLaY"

if let range = strUrl.range(of: "=") {

     let strIdentifier = strUrl.substring(from: range.upperBound)
     print("Identifier:\(strIdentifier)")


 }

Swift Trim Strings, func trim() -> String {. return trimmingCharacters(in: .whitespaces). } /// Strips (or removes) given character from string. ///. /// - Parameters: /// - character: String of  You need to use the trimmingCharacters(in:) method and provide a list of the characters you want to trim. If you're just using whitespace (tabs, spaces and new lines) you can use the predefined whitespacesAndNewlines list of characters, like this: let str = " Taylor Swift " let trimmed = str.trimmingCharacters(in: .whitespacesAndNewlines)

trim, strip and split, regex for Swift String · GitHub, To remove the last character from a string we need to use the removeLast() method in swift. Here is an example that removes the last character  String doesn't have a random-access index, so you can't just go straight to the 3rd character. This is because full unicode support means you can't go forward a defined number of bytes to find a specific character, because characters can be of variable length.

How to remove last character from a string in Swift, You wouldn't be using any String methods that way. To remove the last n characters from a text string, you can use a formula based on the LEFT and LEN​  The String.TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed. The order of the elements in the character array does not affect the trim operation. The trim stops when a character not specified in the array is found.

Remove specific character from string swift 4, A thread to discuss trim/pad/center/etc. cc @Michael_Ilseman substring, stringprotocol, unicodescalarview; A standards based decision of what the Set​<Character> value because it'd be a regression over NSString 's  - Check if string contains string - Check if string has prefix - Check if string has suffix - Check if string is empty - Compare 2 strings - Concatenate strings - Convert a string to Lowercase - Convert a string to Uppercase - Generate a Unique Identifier - Get substring from range - Get the length of a string - Loop through characters in string

Comments
  • Possible duplicate of Remove underscore From a String in xcode
  • Hi Leo Dabus. Can you please check my question if when you available. :-). I did't get any help yet. I think little bit mistake are in my code. I think you can resolve it. Thanks. stackoverflow.com/questions/38561293/…