Objective-C 中的字符串操作
这很难描述,但我目前正在从数据库中捕获一个字符串,该字符串可以是 1-4 个字符长,但是我希望始终显示 4 个字符,所以如果我得到一个 34 的字符串,我想要它是 0034。
我已经设置了一个方法来捕获该字符串,所以现在我只需要弄清楚如何执行此操作。然后我计划做的就是将该字符串输入 NSArray,这样我就可以将数组的每个 [i'th] 发送到 4 个不同的方法来控制我的应用程序中的动画。
之所以采用字符串格式,是因为由于应用程序中的各种格式原因,我必须将其从十六进制转换为整数再转换为字符串。
这是我到目前为止的代码。建议/解决方案将非常感谢,我很新,很难找到诸如字符串操作等之类的解决方案。//
//... 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{
//...
已更新...发生了奇怪的事情。 到目前为止,这是我的方法。
//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");
}
这是输出。
[会议开始于2011-06-19 16:18:45 +1200.]2011-06-19 16:18:54.615 日产代码0.1[4298:207] 731 2011-06-19 16:18:54.616 日产Code0.1[4298:207] 79043536 2011-06-19 16:18:54.617 日产Code0.1[4298:207] 79043536 2011-06-19 16:18:54.617 nissanCode0.1[4298:207] 你好,来自 formatMyNumber 方法
this is hard to describe but I am currently catching a string from a database, this string can be 1-4 characters long, however I am wanting to always display 4 characters, so if i get say a string back that is 34, i want it to be 0034.
I have set up a method to catch the string so now I just need to figure out how to do this. what I then plan to do is feed that string into a NSArray so I can send each [i'th] of the array off to 4 differetn methods that control animations in my app.
The reason its in string format is because I have had to bounce it round from hex, to int to string for various formatting reasons within the application.
this is my code i have so far. Suggestions/solutions would be great thankyou, I am so new its hard to find solutions for stuff like string manipulation etc..
//... 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... weird stuff happening.
here is my method so far.
//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");
}
this is the output.
[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至于字符串前面的零的数量,有几种方法可以做到这一点。我建议:
NSString *myString = [NSString stringWithFormat:@"%04d",[dataString intValue]];
As far as the number of zeros preceding your string goes there are a couple of ways to do this. I'd suggest:
NSString *myString = [NSString stringWithFormat:@"%04d",[dataString intValue]];
您是否可以使用整数形式而不是字符串形式的数字?如果是这样,那么使用
[NSString stringWithFormat:@"%04d", number]
就非常容易了。请参阅 此处获取可能的格式说明符的列表。Is it possible you could have the number in integer form instead of string form? If so, it's pretty easy to use
[NSString stringWithFormat:@"%04d", number]
. See here for a list of the possible format specifiers.查看stringWithFormat: 可以。我知道你提到你的数字是 NSString,但如果它们是整数,或者你将它们转换回整数,下面的方法可能会成功。修改以下内容以最适合您的需要:
See what stringWithFormat: can do. I realize you mentioned your numbers are NSStrings, but if they were ints, or you convert them back to ints, the following may do the trick. Modify the following to best suit your need: