Remove spaces from the string using Swift.

Source: Internet
Author: User

Remove spaces from the string using Swift.

During the test interview, the following problem occurs frequently: Remove extra spaces from a string because English is segmented by spaces, and URLs usually contain many special characters, therefore, this is a very common practice. Like OC, NSCharacterSet can also be used in swift. There are two common cases for string space deletion:
1. Delete the leading space of the string:

var str = "  Hello, play    ground    "let whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet()str = str.stringByTrimmingCharactersInSet(whitespace)

There are many NSCharacter methods that can be used to set different filtering conditions. Here, the whitespaceAndNewlineCharacterSet method is used to indicate spaces. The method stringByTrimmingCharactersInSet is shown in the method name. It is used to adjust the output of the string and input an NSCharacter type. The above code works as follows:

We can see that only spaces at the beginning and end of the string are removed.
2. Remove the spaces at the beginning and end of the string, and retain only one consecutive internal space:
This situation is also called space extrusion. First, execute the above Code to remove the space at the beginning and end, and then call another method to save the string as an array:

var tempArray = str.componentsSeparatedByCharactersInSet(whitespace)

The function of this method is to split the string in the space and store it into an array.

Now each space in the string has been separately saved as an element in the array, and then deleted using filter:

tempArray = tempArray.filter{$0 != ""}

The current tempArray is as follows:

The last step is to splice the array back to a normal string. Note that the squeeze is not deleted, and the deletion will affect the word order, while the squeeze will only delete unnecessary spaces, therefore, when splicing, add a space in the original place where space exists. Use the join method. Note that the join method is unique. To splice an array into a string, if some characters are used for concatenation, the join method is called for the concatenation string to pass in the array (a bit strange ):

str = " ".join(tempArray)

Final effect:

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.