Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa - Trim all leading whitespace from NSString

(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.

like image 650
SirRatty Avatar asked Jul 08 '10 03:07

SirRatty


People also ask

How do you check if a line is all spaces in C?

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.

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

How do I remove the white space from a string in Objective C?

For those who are trying to remove space in the middle of a string, use [yourString stringByReplacingOccurrencesOfString:@" " withString:@""] .

How to trim leading whitespace from each line of a string?

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.

Does string trim remove newline in C++?

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++?

How to remove the leading whitespace from a string in Kotlin?

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.

How to remove all whitespaces before and after a string in Python?

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.


4 Answers

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 
like image 56
John Franklin Avatar answered Nov 13 '22 07:11

John Franklin


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.

like image 44
Hejazi Avatar answered Nov 13 '22 06:11

Hejazi


This code is taking blanks.

NSString *trimmedText = [strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"%@",trimmedText);

like image 36
aybars Avatar answered Nov 13 '22 07:11

aybars


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];
}
like image 32
Yosi Taguri Avatar answered Nov 13 '22 07:11

Yosi Taguri