gpt4 book ai didi

iphone - objective-c 中的字符串操作

转载 作者:行者123 更新时间:2023-11-28 18:44:35 26 4
gpt4 key购买 nike

这很难描述,但我目前正在从数据库中捕获一个字符串,这个字符串的长度可以是 1-4 个字符,但是我希望始终显示 4 个字符,所以如果我返回一个 34 的字符串,我希望它是 0034。

我已经设置了一个方法来捕获字符串,所以现在我只需要弄清楚如何做到这一点。然后我打算做的是将该字符串输入 NSArray,这样我就可以将数组的每个 [i'th] 发送到 4 个不同的方法来控制我的应用程序中的动画。

它是字符串格式的原因是因为在应用程序中出于各种格式原因,我不得不将它从十六进制转换为 int 到字符串。

这是我目前的代码。建议/解决方案将非常感谢,我是新手,很难找到字符串操作等问题的解决方案。

//... other method I am getting the string from/.
[self formatMyNumber:dataString];
///..


-(void)formatMyNumber:(NSString *)numberString{
//resultLabel.text = numberString; //check to make sure string makes it to here.
//NSLog(@"hello From formatMyNumber method"); //check
}

//..
//the with send off each character to 4 animation methods that accept integers.
- (void)playAnimationToNumber:(int)number{
//...

//UpDated...奇怪的事情发生了。到目前为止,这是我的方法。

//Number Formatter
-(void)formatMyNumber:(NSString *)numberString{


NSLog(@"This is what is passed into the method%@",numberString);
int tempInt = (int)numberString;
NSLog(@"This is after I cast the string to an int %i",tempInt);

//[NSString alloc] stringWithFormat:@"%04d", numberString];
NSString *tempString = [[NSString alloc] initWithFormat:@"%04d", tempInt];

NSLog(@"This is after I try to put zeros infront %@",tempString);

//resultLabel.text = tempString;
//NSLog(@"hello From formatMyNumber method");
}

这是输出。

[Session started at 2011-06-19 16:18:45 +1200.] 2011-06-19 16:18:54.615 nissanCode0.1[4298:207] 731 2011-06-19 16:18:54.616 nissanCode0.1[4298:207] 79043536 2011-06-19 16:18:54.617 nissanCode0.1[4298:207] 79043536 2011-06-19 16:18:54.617 nissanCode0.1[4298:207] hello From formatMyNumber method

最佳答案

就字符串前面的零数而言,有几种方法可以做到这一点。我建议:

NSString *myString = [NSString stringWithFormat:@"%04d",[dataString intValue]];

关于iphone - objective-c 中的字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com