2017-07-11 36 views
0

我想知道如何能夠找到所有數字如下。掃描目標中的數字 - C

輸入

NSString* input = @ "1m3s" 

所需的輸出

@[@"1" @"3"] 

我曾嘗試以下方法

NSArray *arr = [input componentsSeparatedByCharactersInSet: 
         [NSCharacterSet characterSetWithCharactersInString:@"ms"]]; 

它返回我

@[@"1",@"3",@"@"] 

有沒有更好的方法來解決這個問題,有什麼建議?

儘管這個問題已經被標記爲重複,我已經測試了以下answer,但沒有奏效。

更新

這可能是任何字符串值。

如果輸入@"11m32s"@11m32,然後所需的輸出將@[@"11", @"32"];

如果輸入的是@"11x32"@11x32y,然後所需的輸出將@[@"11", @"32"];

+0

查看NSString方法'componentsSeparatedByCharactersInSet'並將其輸出傳遞給您已經做的事情。 –

+0

我試過了,請看我更新的問題。 – hotspring

+0

你想讓你的數字成爲'NSArray'? – nayem

回答

2

嘗試:

NSString* input = @ "1m3s"; 
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; 
NSArray *outArray = [input componentsSeparatedByCharactersInSet:nonDigitCharacterSet]; 
outArray = [outArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; 
+1

如果輸入'[11m32s]'那麼失敗。所需的輸出'[11,32]'。我已經提高了你的努力。 – hotspring

+0

謝謝!但是當我輸入'[11m32s]'時,我得到了結果Array:( 11,)與相同的代碼 –

+0

我也測試了'11m32s'&'11m32',並得到(11,32)結果數組 –

0

我想的是有幫助的,你下面的代碼爲我工作。

NSString *originalString = @"1m3s"; 
    NSMutableArray *arr = [[NSMutableArray alloc] init]; 
    for (int i=0; i<[originalString length]; i++) { 
     if (isdigit([originalString characterAtIndex:i])) { 
      NSMutableString *strippedString = [NSMutableString stringWithCapacity:10]; 
      [strippedString appendFormat:@"%c",[originalString characterAtIndex:i]]; 
      [arr addObject:strippedString]; 
     } 
    } 
    NSLog(@"%@",arr.description); 
+0

「%c」的使用只適用於ASCII(8位)字符。但'characterAtIndex'返回16位字符。至少使用'%C'。 – rmaddy

+0

@rmaddy我會更新我的答案。 –

0

使用下面的代碼:

NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; 
NSArray *outArray = [[input componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""]; 
+2

輸出不是'NSArray'? – hotspring

+0

是的,輸出是陣列..我更新了我的答案。 –

0

使用這個方便的方法:

+ (NSArray *)extractNumberFromText:(NSString *)text 
{ 
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; 
    NSMutableArray *numberArray = [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet]mutableCopy]; 
    [numberArray removeObject:@""]; 
    return numberArray; 
} 
+0

這也返回給我'@ [@「1」,@「3」,@「@」]' – hotspring

+0

查看編輯@hotspring – nayem

+0

爲什麼你手動刪除'「」',滿意嗎? – hotspring

0

這裏是代碼,讓你的願望輸出:

NSString* input = @"1m3s"; 

     NSString *newString = [[input componentsSeparatedByCharactersInSet: 
           [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
           componentsJoinedByString:@""]; 


     NSMutableArray *array = [NSMutableArray array]; 
     for (int i = 0; i < [newString length]; i++) { 
      NSString *ch = [newString substringWithRange:NSMakeRange(i, 1)]; 
      [array addObject:ch]; 
     } 


     NSLog(@"%@",array.description); 
0

您也可以使用此代碼波紋管。

NSString * String = @"24fsd35rf"; 
NSArray* Array = [String componentsSeparatedByCharactersInSet: 
           [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]; 
NSLog(@"%@",[Array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]]); 
2

要提取數字,您可以使用正則表達式。

NSString* input = @ "1m3s"; 
NSString *pattern = @"\\d+"; // searches for one or more digits 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil]; 
NSArray *matches = [regex matchesInString:input options:0 range:NSMakeRange(0, input.length)]; 
NSMutableArray *result = [[NSMutableArray alloc] init]; 
for (NSTextCheckingResult *match in matches) { 
    [result addObject: [input substringWithRange:match.range]]; 
} 
NSLog(@"%@", result); 

或者,如果你想更具體,在表達##m##s使用

NSString* input = @ "1m3s"; 
NSString *pattern = @"(\\d+)m(\\d+)s"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil]; 
NSTextCheckingResult *match = [regex firstMatchInString:input options:0 range:NSMakeRange(0, input.length)]; 
if (match) { 
    NSArray *result = @[[input substringWithRange:[match rangeAtIndex:1]], [input substringWithRange:[match rangeAtIndex:2]]]; 
    NSLog(@"%@", result); 
} 
+0

如果輸入爲「@」11x32「或」@ 11x32y「,如我在問題中提到的那樣,它可以是任何字符? – hotspring

+0

第一版涵蓋兩者。它考慮任何在字符串中出現的一個或多個數字。 – vadian

+0

使用正則表達式是完全矯枉過正和緩慢。只需使用NSScanner即可。 –

3

提取號碼使用NSScanner將允許您掃描花車爲好。另外你的數組直接用NSNumber而不是NSString填充。

NSString * input = @ "12m32s"; 
NSScanner * aScanner = [NSScanner scannerWithString:input]; 
NSInteger aInt; 
NSMutableArray * result = [NSMutableArray array]; 
while (!aScanner.isAtEnd) 
{ 
    if ([aScanner scanInteger:&aInt]) 
    { 
     [result addObject:@(aInt)]; 
    } 
    if (!aScanner.isAtEnd) 
    { 
     aScanner.scanLocation += 1; 
    } 
} 
NSLog(@"%@",result); 
+0

這是一個更好的解決方案。 –