2014-02-11 103 views
0

我試圖操作NSString。我想整理輸出。如果一行中有多個空格,則應該用換行符替換它們。用換行符替換多個空格,但留下單個空格

NSString *myString = @"Name: Tom Smith   Old address  street name : 31 Fox Road  Dixton  0000"; 

我期望從NSLog()輸出:

 
Name: Tom Smith     
Old address  
street name : 31 Fox Road     
Dixton   
0000 

這裏的邏輯有點我一直在努力。我不確定它是否正確。

if (word_spacing > 1) 
     insert word in new line "\n" 
    else 
     carry on from the same line 
+0

你檢查嗎? http://stackoverflow.com/questions/6608420/how-to-remove-whitespace-in-a-string – MrBr

+0

@ MrBr我有,它不給我那個非常相同的期望輸出 –

+1

這需要更多的信息.​​.是你試圖在標籤上顯示這個字符串?你得到的產量是多少?並且是這個字符串硬編碼或你得到它像這樣從一些web服務? –

回答

0

如果你有串之間的不可預測的,你想new line來替代多個空格,則你應該使用正則表達式。你使用的正則表達式不起作用,因爲它會選擇空間one or more time但實際上你想挑選two or more times

我知道這個線程已經解決,但看看這個樣本,以及:

NSString *myString = @"Name: Tom Smith   Old address  street name : 31 Fox Road  Dixton  0000"; 

NSError *error = nil; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s{2,}" options:NSRegularExpressionCaseInsensitive error:&error]; 

NSArray *arr = [regex matchesInString:myString options:NSMatchingReportCompletion range:NSMakeRange(0, [myString length])]; 

arr = [[arr reverseObjectEnumerator] allObjects]; 

for (NSTextCheckingResult *str in arr) { 
    myString = [myString stringByReplacingCharactersInRange:[str range] withString:@"\n"]; } 

NSLog(@"%@", myString); 

輸出日誌:

Name: Tom Smith 
Old address 
street name : 31 Fox Road 
Dixton 
0000 

Live Demo

+1

當然,Regex是正確的解決方案,但爲什麼不使用' - [NSRegularExpression stringByReplacingMatchesInString:options:range:withTemplate:]'? –

+0

這是一個可能的選擇。 – NeverHopeless

+0

謝謝無望 –

0

你可以給它使用try:

NSArray *results = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":"]]

至極返回一個包含已在一組給定被除以字符接收字符串數組。

一旦你剛纔循環結果遞增2,你會得到鍵值對。然後,你可以做一些利用價值微調每個鍵值對的:

NSString *trimmedString = [string stringByTrimmingCharactersInSet: 
            [NSCharacterSet whitespaceCharacterSet]]; 
0

你可能分裂由你想要的字符的字符串(在你的情況不止一個空格)這樣的:

NSArray * components = [myString componentsSeparatedByString: @" "]; 

然後你就可以打印出每個部件跟着一個換行符:

for (NSString * component in components) { 
    NSLog(@"%@\n",component); 
} 
2

您可以通過使用NSCharacterSetcomponentsSeparatedByString做到這一點。

解決方案:

// Your string 
NSString *myString = @"Name: Tom Smith   Old address  street name : 31 Fox Road  Dixton  0000"; 

// Seperating words which have more than 1 space with another word 
NSArray *components = [myString componentsSeparatedByString:@" "]; 
NSString *newString = @""; 
NSString *oldString = @""; 
for (NSString *tempString in components) 
{ 
    // Creating new string 
    newString = [oldString stringByAppendingString:[tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 

    // Avoiding new line characters or extra spaces contained in the array 
    if (![oldString isEqualToString:newString]) 
    { 
     newString = [newString stringByAppendingString:@"\n"]; 
     oldString = newString; 
    } 
} 
NSLog(@"%@",newString); 

您可以使用NSRegularExpression

NSString *pattern = [NSString stringWithFormat:@" {2,%d}",[myString length]]; 

NSError *error = nil; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error]; 

NSString *output = [regex stringByReplacingMatchesInString:myString options:0 range:NSMakeRange(0, [myString length]) withTemplate:@"\n"]; 
NSLog(@"%@", output); 
相關問題