Remove all whitespaces from NSString

后端 未结 11 1750
北荒
北荒 2020-12-04 06:23

I\'ve been trying to get rid of the white spaces in an NSString, but none of the methods I\'ve tried worked.

I have \"this is a test\" and

相关标签:
11条回答
  • 2020-12-04 07:04

    I strongly suggest placing this somewhere in your project:

    extension String {
        func trim() -> String {
            return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        }
    
        func trim(withSet: NSCharacterSet) -> String {
            return self.stringByTrimmingCharactersInSet(withSet)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 07:09

    That is for removing any space that is when you getting text from any text field but if you want to remove space between string you can use

    xyz =[xyz.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    

    It will replace empty space with no space and empty field is taken care of by below method:

    searchbar.text=[searchbar.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
    
    0 讨论(0)
  • 2020-12-04 07:10

    stringByReplacingOccurrencesOfString will replace all white space with in the string non only the starting and end

    Use

    [YourString stringByTrimmingCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]]
    
    0 讨论(0)
  • 2020-12-04 07:12

    This for me is the best way SWIFT

            let myString = "  ciao   \n              ciao     "
            var finalString = myString as NSString
    
            for character in myString{
            if character == " "{
    
                finalString = finalString.stringByReplacingOccurrencesOfString(" ", withString: "")
    
            }else{
                finalString = finalString.stringByReplacingOccurrencesOfString("\n", withString: "")
    
            }
    
        }
         println(finalString)
    

    and the result is : ciaociao

    But the trick is this!

     extension String {
    
        var NoWhiteSpace : String {
    
        var miaStringa = self as NSString
    
        if miaStringa.containsString(" "){
    
          miaStringa =  miaStringa.stringByReplacingOccurrencesOfString(" ", withString: "")
        }
            return miaStringa as String
    
        }
    }
    
    let myString = "Ciao   Ciao       Ciao".NoWhiteSpace  //CiaoCiaoCiao
    
    0 讨论(0)
  • 2020-12-04 07:13

    stringByTrimmingCharactersInSet only removes characters from the beginning and the end of the string, not the ones in the middle.

    1) If you need to remove only a given character (say the space character) from your string, use:

    [yourString stringByReplacingOccurrencesOfString:@" " withString:@""]
    

    2) If you really need to remove a set of characters (namely not only the space character, but any whitespace character like space, tab, unbreakable space, etc), you could split your string using the whitespaceCharacterSet then joining the words again in one string:

    NSArray* words = [yourString componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString* nospacestring = [words componentsJoinedByString:@""];
    

    Note that this last solution has the advantage of handling every whitespace character and not only spaces, but is a bit less efficient that the stringByReplacingOccurrencesOfString:withString:. So if you really only need to remove the space character and are sure you won't have any other whitespace character than the plain space char, use the first method.

    0 讨论(0)
  • - (NSString *)removeWhitespaces {
      return [[self componentsSeparatedByCharactersInSet:
                        [NSCharacterSet whitespaceCharacterSet]]
          componentsJoinedByString:@""];
    }
    
    0 讨论(0)
提交回复
热议问题