来自NSString的子串

时间:2013-02-08 06:46:22

标签: ios nsstring substring

我正在使用NSString,我希望得到一个包含字符串前20个字符的子字符串。我怎么能这样做?

5 个答案:

答案 0 :(得分:48)

您可以使用substringToIndex

NSString *mystring = @"This is a test message having more than 20 characters";
NSString *newString = [mystring substringToIndex:20];
NSLog(@"%@", newString);

答案 1 :(得分:33)

NSString *str = @"123456789012345678901234";
NSString *subStr = [str substringWithRange:NSMakeRange(0, 20)];

答案 2 :(得分: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];

答案 3 :(得分:1)

试试这个

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

希望它可以帮助你..

答案 4 :(得分:0)

请查看以下内容:

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

对你有帮助。

干杯!