简体   繁体   English

确定字符串的第一个字符是否为1

[英]Determine if the first character of a string is 1 or not

Basically I'm working on something that looks like the phone apps keypad. 基本上我正在做一些看起来像手机应用程序键盘的东西。 Everything else is inputting as it should but what I'm trying to figure out is how on this last one to get it when a 1 is inputted first that the number would look like 1 (555) 555-5555. 其他所有东西都按照它应该输入,但是我想弄清楚的是当最后一个输入1时,如果最后一个输入该数字将看起来像1(555)555-5555。 I'm sure my code will need some tweaking to the substringfromindexes to get it to match up, but what I'm trying to get to happen now is just to check if the entered strings first number is a one or not. 我确定我的代码需要对substringfromindexes进行一些调整以使其匹配,但我现在想要发生的只是检查输入的字符串第一个数字是否为1。 Because if it is not it will just bypass this part of code. 因为如果不是,它将绕过这部分代码。

else if ([self.self.enteredPhoneNumberString length] == 11 && [self.enteredPhoneNumberString characterAtIndex:0] == '1') {
        NSString *firstNum = [self.enteredPhoneNumberString substringToIndex:1];
        NSString *createFirst = [self.enteredPhoneNumberString substringFromIndex:3];
        NSString *firstThree = [createFirst substringToIndex:3];
        NSString *createSecond = [self.enteredPhoneNumberString substringFromIndex:3];
        NSString *secondThree = [createSecond substringToIndex:3];
        NSString *lastSet = [self.enteredPhoneNumberString substringFromIndex:6];
        self.label.text = [NSString stringWithFormat:@"%@ (%@) %@-%@", firstNum, firstThree, secondThree, lastSet];
    }

If you know of the way to adjust the substrinFromIndex to get the effect I'm going for too by all means that would save me time tampering until I find the right order. 如果你知道调整substrinFromIndex的方法来获得我想要的效果,那将节省我的时间篡改,直到我找到正确的顺序。 Thanks in advance for all help. 在此先感谢您的帮助。 :) :)

Oh and btw with the above code it did a breakpoint when I got to the this part in the code. 哦,顺便说一句上面的代码,当我到达代码中的这一部分时,它做了一个断点。

FIX: 固定:

Thanks for all the help it works now. 感谢您现在所有的帮助。 Ended up doing this and for some reason the breakpoint doesnt happen when it is done here. 结束这样做,由于某种原因断点在这里完成时不会发生。

else if ([self.self.enteredPhoneNumberString length] >= 11 ) {
    if ([self.enteredPhoneNumberString characterAtIndex:0] == '1' && [self.self.enteredPhoneNumberString length] == 11) {
        NSString *firstNum = [self.enteredPhoneNumberString substringToIndex:1];
        NSString *createFirst = [self.enteredPhoneNumberString substringFromIndex:1];
        NSString *firstThree = [createFirst substringToIndex:3];
        NSString *createSecond = [self.enteredPhoneNumberString substringFromIndex:4];
        NSString *secondThree = [createSecond substringToIndex:3];
        NSString *lastSet = [self.enteredPhoneNumberString substringFromIndex:7];
        self.label.text = [NSString stringWithFormat:@"%@ (%@) %@-%@", firstNum, firstThree, secondThree, lastSet];
    }
    else{
       self.label.text = [NSString stringWithFormat:@"%@", enteredPhoneNumberString]; 
    }
}

And ya it does seem pointless to have to check if the length is 11 again, but because it was causing issues the previous way I didnt really have much of a choice because I needed it to know whether it exceeded 11 or not so it could start doing that else. 而且,检查长度是否再次为11似乎毫无意义,但因为它导致问题以前的方式我没有太多的选择,因为我需要它知道它是否超过11或所以它可以开始做那个。

It could be the if statement above where your code starts is true... 它可能是上面的if语句,你的代码开始是真的......

else if ([self.self.enteredPhoneNumberString length] == 11 && [self.enteredPhoneNumberString characterAtIndex:0] == '1') {

needs some cleaning up. 需要一些清理。 You shouldn't check for length 11 here, just check for length > 0 and whether first character is a 1. Do the necessary formatting, trim white space and (), then check the length. 你不应该在这里检查长度11,只检查长度> 0以及第一个字符是否为1.做必要的格式化,修剪空格和(),然后检查长度。 Also, self.self is a potential problem, drop the second self 另外, self.self是一个潜在的问题,放弃第二个自我

Please disregard CocoaFu's comment his solution is better, but the comment is misleading. 请忽略CocoaFu的评论,他的解决方案更好,但评论有误导性。

In the following code, my log statement shows up in the console 在以下代码中,我的日志语句显示在控制台中

NSString * test = [NSString stringWithString:@"test"];
unichar c = [adjusted characterAtIndex:0];

if (c == 't')   {
    NSLog(@"can compare char");
}
if [[self.enteredPhoneNumberString substringToIndex:1] isEqualToString:@"1"] {

}

I think this is what you are asking for... 我想这就是你要求的......

第一个字符最简单的比较:

[self.enteredPhoneNumberString hasPrefix:@"1"]

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

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