Substring from NSString [closed]

2019-03-13 01:09发布

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?

5条回答
家丑人穷心不美
2楼-- · 2019-03-13 01:34

You can use substringToIndex.

NSString *mystring = @"This is a test message having more than 20 characters";
NSString *newString = [mystring substringToIndex:20];
NSLog(@"%@", newString);
查看更多
男人必须洒脱
3楼-- · 2019-03-13 01:37
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楼-- · 2019-03-13 01:38

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!

查看更多
贼婆χ
5楼-- · 2019-03-13 01:39

Try this

NSString *string = @"This is for testing substring from string";
    NSInteger intIndex = 20;
    NSLog(@"%@", [string substringToIndex:intIndex]);

Hope it helps you..

查看更多
forever°为你锁心
6楼-- · 2019-03-13 01:49
NSString *str = @"123456789012345678901234";
NSString *subStr = [str substringWithRange:NSMakeRange(0, 20)];
查看更多
登录 后发表回答