How to remove character from string in swift

How to remove character from string in swift
November 28, 2018 No Comments Development,iOS Development Pushpendra Kumar



If you are looking for the modification of string into the different format, then you are on the correct post. Because here I am going to tell you the concept that how can modify your entire string into the swift language. This protocol will help you bring you out from the different situation.

Let’s suppose you want to change any character with any character into the entire string then you can follow the below code.

func change_string() {
        var my_string = "abcd_fgh"
        my_string = my_string.replacingOccurrences(of: "_", with: "e", options: NSString.CompareOptions.literal, range: nil)
    }




This function is useful for replace the _ with character e. in the same manner if you want to remove the _ or any other character or spacial character from the string then you can replace that character with the blank space. Below you can find the example method.

func change_string() {
        var my_string = "abcd_fgh"
        my_string = my_string.replacingOccurrences(of: "_", with: "", options: NSString.CompareOptions.literal, range: nil)
    }

This function will help you with removing the character from the string. There is another different term you should know about the sting which is given as below.

Swift uses backslash to escape double quotes. Here is the list of escaped special characters in Swift:

\0 (null character)
\\ (backslash)
\t (horizontal tab)
\n (line feed)
\r (carriage return)
\" (double quote)
\' (single quote)

If you want a deep knowledge about the string then go through by the link – String Literals

Good Luck 🙂 Happy Coding


Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *