(have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs)
Q. How can I trim all leading whitespace only from an NSString? (i.e. leaving any other whitespace intact.)
Unfortunately, for my purposes, NSString's stringByTrimmingCharactersInSet method works on both leading and trailing.
Mac OS X 10.4 compatibility needed, manual GC.
C isspace() The isspace() function checks whether a character is a white-space character or not. If an argument (character) passed to the isspace() function is a white-space character, it returns non-zero integer. If not, it returns 0.
A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.
For those who are trying to remove space in the middle of a string, use [yourString stringByReplacingOccurrencesOfString:@" " withString:@""] .
trimMargin is used to trim leading whitespace characters from each line of a string. If the first and the last lines are blank, they will be removed. Note that one parameter is passed to this method : marginPrefix.
After performing trimming in C++, the above string should be: Note: Carriage return (‘r’), horizontal tab (‘t’), vertical tab (‘v’), line feed (‘n’) all are whitespaces. 3. Does String Trim Remove Newline? Yes, it does. That is because when we use the trim () function, the characters that represent newlines are removed. 4. What Is Boost in C++?
As you can see that trimMargin is really a useful function in kotlin for removing the leading whitespace characters of a string. Always keep in mind that marginPrefix is ”|” by default.
Then, a constant typeOfWhitespaces is declared to erase all types of white spaces before and after the string. Moreover, the Str.find_first_not_of (typeOfWhitespaces) will return the index value of the first non-whitespace character so the str.erase function will remove all whitespaces before the returned index.
This creates an NSString category to do what you need. With this, you can call NSString *newString = [mystring stringByTrimmingLeadingWhitespace];
to get a copy minus leading whitespace. (Code is untested, may require some minor debugging.)
@interface NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace; @end @implementation NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace { NSInteger i = 0; while ((i < [self length]) && [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[self characterAtIndex:i]]) { i++; } return [self substringFromIndex:i]; } @end
This is another solution using Regular Expressions (requires iOS 3.2):
NSRange range = [string rangeOfString:@"^\\s*" options:NSRegularExpressionSearch]; NSString *result = [string stringByReplacingCharactersInRange:range withString:@""];
And if you want to trim the trailing whitespaces only you can use @"\\s*$"
instead.
This code is taking blanks.
NSString *trimmedText = [strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",trimmedText);
Here is a very efficient (uses CoreFoundation) way of doing it (Taken from kissxml):
- (NSString *)trimWhitespace {
NSMutableString *mStr = [self mutableCopy];
CFStringTrimWhitespace((CFMutableStringRef)mStr);
NSString *result = [mStr copy];
[mStr release];
return [result autorelease];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With