17

我正在使用NSString并且我想获取它的子字符串,其中包含我的字符串的前 20 个字符。我怎样才能做到这一点?

4

5 回答 5

48

您可以使用substringToIndex.

NSString *mystring = @"This is a test message having more than 20 characters";
NSString *newString = [mystring substringToIndex:20];
NSLog(@"%@", newString);
于 2013-02-08T06:50:47.433 回答
34
NSString *str = @"123456789012345678901234";
NSString *subStr = [str substringWithRange:NSMakeRange(0, 20)];
于 2013-02-08T06:50:30.723 回答
1
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];
于 2013-02-08T06:52:51.053 回答
1

试试这个

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

希望对你有帮助。。

于 2013-02-08T06:56:08.180 回答
0

请检查以下内容:

NSString *strTmp = @"This is test string to check substring.";
NSInteger intIdx = 20;
NSLog(@"%@", [strTmp substringToIndex:intIdx]);

这将对您有所帮助。

干杯!

于 2013-02-08T06:53:14.043 回答