关于编辑 NSString 的建议

发布于 2024-10-15 02:13:31 字数 778 浏览 4 评论 0原文

对于我当前正在制作的应用程序,我需要将字符串分解为字符,然后将每个字符转换为数字。我想到的一种方法是使用以下代码;

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2024-10-22 02:13:31

我不完全清楚你的意图,但我会尝试猜测。您想要做的是逐个字符地迭代字符串,并分析每个字符并将转换结果存储到一个数组中。

// Get length of string
NSUInteger len = [myString length];

// allocate number buffer
NSUInteger *numbers = calloc(len, sizeof(NSUInteger));

// loop through the string's characters and assign to the number array.
for (NSUInteger i = 0; i < len; i++)
{
    unichar thisChar = [myString characterAtIndex:i];

    if (thisChar == 'A')
        numbers[i] = 5;
    else if (thisChar == 'C')
        numbers[i] = 10;
}

// do what you want with the numbers array, and then free it.
free(numbers)

另外,考虑使用查找表将字符转换为数字(如果存在大量字符到数字的转换)。

最后一件事,您不能使用 == 比较字符串,因为这将测试指针相等性,而不是字符串相等性。当你比较字符串时,你应该使用:

if ([someString isEqualToString:anotherString])
    // ... and so on ...

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.

// Get length of string
NSUInteger len = [myString length];

// allocate number buffer
NSUInteger *numbers = calloc(len, sizeof(NSUInteger));

// loop through the string's characters and assign to the number array.
for (NSUInteger i = 0; i < len; i++)
{
    unichar thisChar = [myString characterAtIndex:i];

    if (thisChar == 'A')
        numbers[i] = 5;
    else if (thisChar == 'C')
        numbers[i] = 10;
}

// do what you want with the numbers array, and then free it.
free(numbers)

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:

if ([someString isEqualToString:anotherString])
    // ... and so on ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文