How can I replace newline (\n) sequences with one space.
I.e the user has entered a double newline ("\n\n") I want that replaced with one space (" "). Or the user has entered triple newlines ("\n\n\n") I want that replaced with also one space (" ").
Try this:
NSArray *split = [orig componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
split = [split filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]];
NSString *res = [split componentsJoinedByString:#" "];
This is how it works:
First line splits by newline characters
Second line removes empty items inserted for multiple separators in a row
Third line joins the strings back using a single space as the new separator
3 times more performant than using componentsSeparatedByCharactersInSet
NSString *fixed = [original stringByReplacingOccurrencesOfString:#"\\n+"
withString:#" "
options:NSRegularExpressionSearch
range:NSMakeRange(0, original.length)];
Possible alternative regex patterns:
Replace only space: [ ]+
Replace space and tabs: [ \\t]+
Replace space, tabs and newlines: \\s+
Replace newlines: \\n+
As wattson says you can do this with NSRegularExpression but the code is quite verbose so if you want to do this at several places I suggestion you to do a helper method or even a NSString category with method like -[NSString stringByReplacingMatchingPattern:withString:] or something similar.
NSString *string = #"a\n\na";
NSLog(#"%#", [[NSRegularExpression regularExpressionWithPattern:#"\\n+"
options:0
error:NULL]
stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:#" "]);
Use a regular expression, something like "s/\n+/\w/" (a replace which will match 1 or more newline character and replace with a single white space)
this question has a link to a regex library, but there is NSRegularExpression available too
Related
String: abc2_2fkf-lo
Now I want to use regex to delete the special characters as _ and -
The expect string as I want: abc22fkflo
Use stringByReplacingOccurrencesOfString.
NSString *string = #"abc2_2fkf-lo";
NSString *updated = [string stringByReplacingOccurrencesOfString:#"[-_]" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
This replaces any occurrence of a - or _ character with the empty string.
Note that if you want to match a - character using [ ] in a regular expression, the - needs to be the first character to avoid its normal special use as a character range operator.
This question already has an answer here:
How do you remove extra empty space in NSString?
(1 answer)
Closed 6 years ago.
I am trying to delete the extra white spaces in my string
for exemple
NSString *mystring= #" Alex mona ok";
so after deleting the extra white spaces mastering should look like this
// deleting the first spaces, middle spaces and the last spaces
"Alex mona ok"
Unfortunately, Cocoa's split method is not versatile enough to remove duplicate separators on its own, so you need to write quite a bit of code:
Split your string into words on whitespace
Remove empty entries created for adjacent separators
Join the array back on a single space
Here is the same thing coded in Objective-C:
NSString *mystring= #" Alex mona ok";
NSMutableArray *words = [[mystring componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
[words removeObject:#""];
NSString *res = [words componentsJoinedByString:#" "];
If you only need to remove a certain character like a space use this:
[mystring stringByReplacingOccurrencesOfString:#" " withString:#""]
If you need to remove tabs, spaces, etc. use:
NSArray* newstring = [mystring componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* nospacestring = [newstring componentsJoinedByString:#" "];
This removes all whitespace and then joins the components of the non whitespace back together.
I want to remove Special Characters, Numbers and Double Spaces from a string, but it's removing all spaces. How can I fix it?
Code:
_tfName.text = [[_tfName.text componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:#""];
Strings:
Real String Formated What I want
___________ ________ ___________
Lincon Man LiconMan Licon Man
Name Surname NameSurname Name Surname
09123721*)(%!#ˆ*# *blank* *blank*
You have a lot of options available. A good one is to use NSRegularExpression for the two needs. One to find unwanted characters and remove them and another to find double spaces and remove them.
I recommend this option because you can reuse most of the logic for doing both.
You can also use NSScanner.
You could accomplish the unwanted characters part by using NSCharacterSet to create a set of chars you don't want and remove them with NSString method
– stringByTrimmingCharactersInSet:
that takes an NSCharacterSet argument.
NSString has
– stringByReplacingOccurrencesOfString:withString:
You can pass it a double space string.
Other options exist, but NSRegularExpression is the way to go for the double spaces.
The NSCharacterSet approach is pretty easy for the unwanted characters.
Did you think of passing a white space as the parameter to componentsJoinedByString:
string = [[string componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:#" "];
This may be what you are looking for:
NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:#"[##$.,!\\d]" options:0 error:nil];
NSString *input = #"1234 Nöel* *Smith $!#";
NSString *output = [re stringByReplacingMatchesInString:input
options:0
range:NSMakeRange(0, [input length])
withTemplate:#""];
/* -> Nöel Smith */
Depending if you know what characters to remove or which ones to keep, you can write the regexp like this or the opposite with [^caracters-to-keep]. Just use the needed metacharacters for your case.
I want to make every string look like this:
"this-is-string-one"
So main string could contain:
"This! is string, one"
So basicaly replace !?., whitespace and characters like that with "-".
Something like this is best handled by a regular expression:
NSString *someString = #"This! is string, one";
NSString *newString = [someString stringByReplacingOccurrencesOfString:#"[!?., ]+" withString:#"-" options: NSRegularExpressionSearch range:NSMakeRange(0, someString.length)];
NSLog(#"\"%#\" was converted to \"%#\"", someString, newString);
The output will be:
"This! is string, one" was converted to "This-is-string-one"
The regular expression [!?., ]+ means any sequence of one or more of the characters listed between the square brackets.
Update:
If you want to truncate any trailing hyphen then you can do this:
if ([newString hasSuffix:#"-"]) {
newString = [newString substringToIndex:newString.length - 1];
}
I am splitting an NSString like this: (filter string is an nsstring)
seperatorSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
[seperatorSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
NSMutableArray *words = [[filterString componentsSeparatedByCharactersInSet:seperatorSet] mutableCopy];
I want to put words back into the form of filter string with the original punctuation and spacing. The reason I want to do this is I want to change some words and put it back together as it was originally.
A more robust way to split by words is to use string enumeration. A space is not always the delimiter and not all languages delimit spaces anyway (e.g. Japanese).
NSString * string = #" \n word1! word2,%$?'/word3.word4 ";
[string enumerateSubstringsInRange:NSMakeRange(0, string.length)
options:NSStringEnumerationByWords
usingBlock:
^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSLog(#"Substring: '%#'", substring);
}];
// Logs:
// Substring: 'word1'
// Substring: 'word2'
// Substring: 'word3'
// Substring: 'word4'
NSString *myString = #"Foo Bar Blah B..";
NSArray *myWords = [myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#" "]
];
NSString* string = [myWords componentsJoinedByString: #" "];
NSLog(#"%#",string);
Since you eliminate the original punctuation, there's no way to turn it back automatically.
The only way is not to use componentsSeparatedByCharactersInSet.
An alternative solution may be to iterate through the string and, for each char, check if it belongs to your character set.
If yes, add the char to a list and the substring to another list (you may use NSMutableArray class).
This way, for example, you know that the punctuation char between the first and the second substring is the first character in your list of separators.
You can use the pathArray componentsJoinedByString: method of the array class to rejoin the words:
NSString *orig = [words pathArray componentsJoinedByString:#" "];
How are you determining which words need to be replaced? Instead of breaking it apart in the first place, perhaps using -stringByReplacingOccurrencesOfString:withString:options:range: would be more suitable.
My guess is you may not be using the best API. If you're really worried about words, you should be using a word-based API. I'm a bit hazy on whether that would be NSDataDetector or something else. (I believe NSRegularExpression can deal with word boundaries in a smarter way.)
If you are using Mac OS X 10.7+ or iOS 4+ you can use NSRegularExpression, The pattern to replace a word is: "\b word \b" - (no spaces around word) \b matches a word boundary. Look at methods replaceMatchesInString:options:range:withTemplate: and stringByReplacingMatchesInString:options:range:withTemplate:.
Under 10.6 pr earlier if you wish to use regular expressions you can wrap the regcomp/regexec C-based functions, they support word boundaries as well. However you may prefer to use one of the other Cocoa options mentioned in other answers for this simple case.