简体   繁体   English

如何将NSString转换为NSInteger

[英]how to convert NSString to NSInteger


I have a NSString(eg 'P1') 我有一个NSString(例如'P1')
I have to add 1 in this NSString to get 'P2'. 我必须在这个NSString中添加1来获得'P2'。 How will i do this?? 我该怎么办?
Thanks for any help. 谢谢你的帮助。

You can convert your NSString to an NSInteger like so: 您可以转换NSString一个NSInteger ,像这样:

NSInteger myInteger = [[myString substringWithRange:NSMakeRange(1, 1)] integerValue];

In your case if myString was your string P1, myInteger would be 1. So you could then do: 在你的情况下,如果myString是你的字符串P1, myInteger将是1.所以你可以这样做:

myInteger = myInteger + 1;

Which would then make myInteger 2. To then put that number back after P do: 那将使myInteger 2.然后在P之后将该数字放回:

myString = [NSString stringWithFormat:@"P%i", myInteger];

Now myString is P2. 现在myString是P2。

Split it in to a prefix containing the P and a suffix containing the number. 将其拆分为包含P的前缀和包含该数字的后缀。 How you do this depends on the possible format of your string. 如何执行此操作取决于字符串的可能格式。

Then send intValue to the string. 然后将intValue发送到字符串。

Increment the number 增加数量

Put it all back together with -stringWithFormat: 将所有内容与-stringWithFormat放在一起:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM