簡體   English   中英

如何在目標c中修剪字符串中的特殊字符

[英]How to Trim special Characters in a String in Objective c

我想修剪字符串中特殊字符的出現

String  has :    
prevs: Case Number                                                                    
____________________

在這個我想從字符串中刪除--------這個破折號。我已經嘗試過這樣:

NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"-"];
    NSString *stringNew = [[previousString2 componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:@""];

提前致謝!

嘗試這個:

NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *trimedString = [myString stringByReplacingOccurrencesOfString:@"-" withString:@""];

使用正則表達式,模式[\\\\s_]{4,}搜索4個或更多的空格或下划線字符。

NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:@"[\\s_]{4,}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];

如果下划線字符確實是破折號,則會在模式中替換該字符。

您可以使用以下代碼刪除特殊字符串。

NSString *yourString = @"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:@""];
NSLog (@"Your final string : %@", finalString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM