关于编辑 NSString 的建议
对于我当前正在制作的应用程序,我需要将字符串分解为字符,然后将每个字符转换为数字。我想到的一种方法是使用以下代码;
//Get string length
int stringLength = [myString length];
//Create new variable for "While" loop
int count = stringLength;
//Start "While" loop
while (count != 0) {
//What I want her is for the NSString to be ("letter%i",count) but I don't know how to do this
letter1 = [myString substringWithRange:NSMakeRange(0,stringLenght-count)];
//each letter = 1 so it will move down one letter at a time
count--
}
然后我会有类似的东西;
if (string1 == @"a") {
number1 = 5;
}
if (string2 == @"a") {
number2 = 5;
}
..........
我能够读取从 while 循环外部创建的新字符串吗?任何建议都会非常有帮助。另外,任何其他方式来做这件事也会有帮助。
预先感谢,
乔纳森
For an app that I am currently making I need to break up a string into characters and then convert each of those characters into a number. One way I thought of doing this was by using the following code;
//Get string length
int stringLength = [myString length];
//Create new variable for "While" loop
int count = stringLength;
//Start "While" loop
while (count != 0) {
//What I want her is for the NSString to be ("letter%i",count) but I don't know how to do this
letter1 = [myString substringWithRange:NSMakeRange(0,stringLenght-count)];
//each letter = 1 so it will move down one letter at a time
count--
}
and then I would have something like;
if (string1 == @"a") {
number1 = 5;
}
if (string2 == @"a") {
number2 = 5;
}
..........
Would I be able to read the new strings that I create from outside the while loop? Any suggestions would be very helpful. Also any way to do this another way would also be helpful too.
Thanks in advance,
Jonathan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全清楚你的意图,但我会尝试猜测。您想要做的是逐个字符地迭代字符串,并分析每个字符并将转换结果存储到一个数组中。
另外,考虑使用查找表将字符转换为数字(如果存在大量字符到数字的转换)。
最后一件事,您不能使用
==
比较字符串,因为这将测试指针相等性,而不是字符串相等性。当你比较字符串时,你应该使用:I'm not entirely clear on your intentions, but I'll try and guess. What you want to do is iterate over the string, character-by-character, and analyse each character and store your conversion into an array.
Also, consider using a look-up table to convert a character to a number (if there are a large number of character-to-number conversions).
And just one last thing, you can't compare strings using
==
, because that will test for pointer equality, not for string equality. When you compare strings, you should use: