问题
I am using NSString
and I want to get a substring of it that contains the first 20 characters of my string. How can I do that?
回答1:
You can use substringToIndex
.
NSString *mystring = @"This is a test message having more than 20 characters";
NSString *newString = [mystring substringToIndex:20];
NSLog(@"%@", newString);
回答2:
NSString *str = @"123456789012345678901234";
NSString *subStr = [str substringWithRange:NSMakeRange(0, 20)];
回答3:
NSString *string = @"I am having a simple question.I am having NSString and i want a substring of it that contains first 20 ";
NSString *first20CharString = [string substringToIndex:20];
回答4:
Try this
NSString *string = @"This is for testing substring from string";
NSInteger intIndex = 20;
NSLog(@"%@", [string substringToIndex:intIndex]);
Hope it helps you..
回答5:
Check with below:
NSString *strTmp = @"This is test string to check substring.";
NSInteger intIdx = 20;
NSLog(@"%@", [strTmp substringToIndex:intIdx]);
It will be helpful to you.
Cheers!
来源:https://stackoverflow.com/questions/14767167/substring-from-nsstring