NSString Length Limit

How can I limit the length of an NSString? I would like to leave it below 100 characters /

+4
source share
3 answers

Here is an example of the fundamental size of a control string.

Declare the maximum string length:

int max = 5; 

Suppose you have a list of strings in an array:

 NSArray *strs = [NSArray arrayWithObjects:@"My Little Puppy", @"Yorkerz", nil]; 

Cycle operation:

 [strs enumerateObjectsUsingBlock:^(NSString *str, NSUInteger idx, BOOL *stop) { if (str.length > max) //get a modified string str = [str substringToIndex:str.length-(str.length-max)]; NSLog(@"%@", str); }]; 

Results:

 "My Li" and "Yorke" 

I hope I understood your question correctly.

+7
source

Then you will need to check length: before placing it in an NSString. Then, if the length is greater than 100, you use substringToIndex:

+1
source

Then do not add more than 100 characters.

If you have an existing string that you want to shorten, you can use the substringToIndex: method to create a shorter string.

0
source

All Articles