Substring from NSString [closed]

故事扮演 提交于 2020-01-12 01:46:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!