tas*_*oor 66
这将删除所有空间myString.
NSString *newString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];
Run Code Online (Sandbox Code Playgroud)
Par*_*att 31
以下是从字符串中删除空格的正确记录方法.
whitespaceCharacterSet适用于iOS的Apple文档说:
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
+ (id)whitespaceCharacterSet
Return Value
A character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
Discussion
This set doesn’t contain the newline or carriage return characters.
Availability
Available in iOS 2.0 and later.
Run Code Online (Sandbox Code Playgroud)
您可以使用以下记录的方式:
[yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
希望这对你有所帮助.
如果您需要更多帮助,请告知我们.
gw0*_*gw0 12
可能是将白色空间的折叠序列中的一个答案解决为单个字符和修剪字符串的解决方案:
NSString *whitespaceString = @" String with whitespaces ";
NSString *trimmedString = [whitespaceString stringByReplacingOccurrencesOfString:@" " withString:@""];
Run Code Online (Sandbox Code Playgroud)
如果你想要空格和换行符,那么使用" whitespaceAndNewlineCharacterSet "代替" whitespaceCharacterSet "
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
NSString *trimmedString = [temp.text stringByTrimmingCharactersInSet:whitespace];
NSLog(@"Value of the text field is %@",trimmedString);
Run Code Online (Sandbox Code Playgroud)