ios 来自 NSString 的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14767167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:13:15  来源:igfitidea点击:

Substring from NSString

iosnsstringsubstring

提问by SumiSadiq

I am using NSStringand I want to get a substring of it that contains the first 20 characters of my string. How can I do that?

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

回答by Bijoy Thangaraj

You can use substringToIndex.

您可以使用substringToIndex.

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

回答by Inder Kumar Rathore

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

回答by Rushi

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];

回答by P.J

Try this

尝试这个

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

Hope it helps you..

希望能帮到你。。

回答by Nishant B

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!

干杯!