2011-11-04 4 views
0

바코드 app.So에서 사용자가 텍스트 필드에 바코드를 입력 할 때 일치 할 때 (-) 다음과 같은 문자열에 삽입하십시오. textfield.text = 12345786 그래서 일치하면 1234-5786 으로 변환됩니다. 내 텍스트가 123457869 또는 1234578 일 때 대시 (-)가 제거되지 않고 textfield.text가 1234가 될 때 textfield.text 값에서 삭제되지만 삽입되지 않습니다. 여기 내 코드가 내가 잘못하고있는 곳을 알려주고있다? NSMutableString * a = [NSMutableString stringWithString : textfield.text];textfield iphone에서 문자열을 바꾸는 방법은 무엇입니까?

if([textfield.text floatValue]>=8){ 


    [a insertString: @"-" atIndex: 4]; 
    //textfield.text = [textfield.text stringByReplacingOccurrencesOfString:@"-" withString:@""]; 

    textfield.text = a; 
    NSLog(@"in if"); 

} 
    else if([textfield.text length]<8){ 


    //[a insertString: @"" atIndex: 4]; 
    textfield.text = [textfield.text stringByReplacingOccurrencesOfString:@"-" withString:@""]; 

    NSLog(@"in else"); 
    //NSRange range = {4,5}; 
    //[a deleteCharactersInRange:range]; 
    //textfield .text = a; 
} 

답변

1
NSString *[email protected]"12345678"; 
    NSString * a =[abc substringWithRange:NSMakeRange(0, 4)]; 
    NSString *b =[abc substringWithRange:NSMakeRange(0, 8)]; 
    abc =[NSString stringWithFormat:@"%@-%@",a,b]; 
    NSLog(@"%@",abc); 
관련 문제