2014-02-21 36 views
2
NSString *qwer2 = [qwer1 stringByReplacingOccurrencesOfString:@"count '0','" withString:@""]; 

NSString *qwer4 = [qwer2 stringByReplacingOccurrencesOfString:@"count '1','" withString:@""]; 

NSString *qwer = [qwer4 stringByReplacingOccurrencesOfString:@"count '2','" withString:@""]; 

如何用任何數字替換字符串?stringByReplacingOccurrencesOfString循環?

計數 '0',計數 '1',計數 '2'

計數 '的任何數字'

我已經嘗試過的循環:

for (int i =0; i<100; i++) 

的NSString * qwer = [qwer4 stringByReplacingOccurrencesOfString:@「count'i','」withString:@「」];

+0

這個問題有點不清楚。你是否試圖生成一個字符串列表,說Count 1 Count 2 Count 2等? (也應該用Objective-C來標記這個帖子) – Jeef

+0

不,我想替換字符串「count」0,'' - >「」 – user3276854

+0

並且有很多count'0'count'1'.... count'x' – user3276854

回答

1

這將幫助你:

NSString *qwer1 = @"My string count '0', count '1', count '2', count '3', count '4',"; 
for (int i =0; i<5; i++) { 
    NSString *count = [NSString stringWithFormat:@"count '%i',", i]; 
    qwer1 = [qwer1 stringByReplacingOccurrencesOfString:count withString:@""]; 
} 
NSLog(@"%@", [qwer1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]); 
+0

不,它不會。 – Desdenova

+0

檢查編輯過的一個。 –

+0

是的,這更像是它,唯一的問題是你離開了很多需要清潔的空白區域。儘管把我的倒票轉換爲了投票。 – Desdenova

4
NSString *replaced = [string stringByReplacingOccurrencesOfString:@"count '\\d+','" 
          withString:@"" 
           options:NSRegularExpressionSearch 
           range:NSMakeRange(0, string.length)]; 

應該做的伎倆。 「\ d +」是一個匹配一個或多個數字的正則表達式模式。

+0

雖然小字體錯誤,但它應該是'@'count'\\ d',「',它會留下空格。 – Desdenova

+1

@Desdenova:'「\\ d +」'用於*一個或多個*數字,因此也可以匹配'「count'10'」或'「count'9999'」'。 - 我不知道你說的是哪個白色空間。我剛剛提出了這個問題的模式''''0',''''。如果OP顯示輸入和預期輸出的完整示例,則可以對其進行改進。 –

+0

按排字錯誤我的意思是額外的'''結尾。你說'\ d +'是正確的,用加號和不加號都試過,好像兩個都在工作。昏迷之前和之後的空間標記,記錄一個長度,你會看到。 – Desdenova

相關問題