Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString length limit

How can I limit the length of a NSString? I would like to keep it below to equal to 100 characters/

like image 211
Joey Avatar asked Aug 23 '10 02:08

Joey


People also ask

How long can a swift string be?

NSUIntegerMax is 2^32 - 1 and NSString can hold a little over 4.2 billion characters.

How do I trim a string to a certain length in Swift?

string-truncate.swift Truncates the string to the specified length number of characters and appends an optional trailing string if longer. - Parameter trailing: A 'String' that will be appended after the truncation. - Returns: 'String' object. let str = "I might be just a little bit too long".

What does NSString mean?

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


1 Answers

Here is the sample of fundamental manipulate string size.

Declare maximum string length:

int max = 5;

Let's assume have list of strings in array:

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

Loop 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"

Hope, I understood right from your question.

like image 82
Yoon Lee Avatar answered Sep 22 '22 02:09

Yoon Lee