I want to trim the occurence of special characters in a string
String has :
prevs: Case Number
____________________
In this i want to remove -------- this dash from this string .I have tried like this :
NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:#"-"];
NSString *stringNew = [[previousString2 componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:#""];
Thanks in Advance!
Try This:
NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSString *trimedString = [myString stringByReplacingOccurrencesOfString:#"-" withString:#""];
Use regular expression, the pattern [\\s_]{4,} searches for 4 and more whitespace or underscore characters.
NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:#"[\\s_]{4,}" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];
If the underscore characters are really dashes reaplace the character in the pattern.
You can use below code to remove your special string.
NSString *yourString = #"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:#""];
NSLog (#"Your final string : %#", finalString);
I have a string that looks like this:
(
TEST STRING
)
I want to remove the parentheses and the white spaces between the parentheses, but not remove the white space within the string.
Basically, I just want the string. I looked into trimming and replacing, but I don't think they applied here.
Any hint on how to go on about solving this problem?
Thanks
This should work:
NSString *originalString = #"( TEST STRING )";
NSString *newString = [originalString stringByReplacingOccurrencesOfString:#"( " withString:#""];
newString = [newString stringByReplacingOccurrencesOfString:#" )" withString:#""];
I would consider using (NSRegularExpressionSearch | NSAnchoredSearch)
something like this (I'm writing code on the fly so its probably not correct.
NSRange rng = [myString rangeOfString:#"([ \t\n\r]*" options:(NSRegularExpressionSearch | NSAnchoredSearch)];
NSRange rng = [myString rangeOfString:#"[ \t\n\r]*)" options:(NSRegularExpressionSearch | NSAnchoredSearch | NSBackWardsSearch)];
if ( rng.length && rng2.length )
{
myString = [myString substringToIndex:rng2.location];
myString = [myString substringFromIndex:rng.location];
}
Remove the ()
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"()"]];
And trim the white space.
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSCharacterSet *unwantedChar = [NSCharacterSet characterSetWithCharactersInString:#" "#"\"-[}()\n "];
NSString *requiredString = [[string componentsSeparatedByCharactersInSet:unwantedChar] componentsJoinedByString: #""];
how to store Height Value separately "6ft 7inch"
6ft in one string and 7inch in another string.
6 and 7 will be dynamically changes ft inch will be constant.
I assume there will always be a single space character separating the 2 height values that you want to extract. You can use -componentsSeparatedByString method to turn the string into array of strings separated by the space character
NSString *originalString = #"6ft 7inch";
NSArray *separatedStrings = [originalString componentsSeparatedByString:#" "];
NSString *feetString = separatedStrings[0];
NSString *inchString = separatedStrings[1];
If there could be cases where there are no spaces in between you'll have to find the index of the key character set and then create substrings using them. Let me know if you need to know how
You just need to split the string.
NSString *combineString=#"6ft 7inch";
NSArray *stringsArray=[combineString componentsSeparatedByString:#" "];
if ([stringsArray count ]> 0) {
//here is the ft string
NSString *ftString=[stringsArray objectAtIndex:0];
if ([stringsArray count ]> 1)
{
//here is the inch string
NSString *inchString=[stringsArray objectAtIndex:1];
}
else
{
NSLog(#"Inch value not found");
}
}
else
{
NSLog(#"Empty String");
}
There are many ways to accomplish that. However, in your case it is the easiest way, so separate the string at the space:
NSString *string = #"6ft 7inch";
NSArray *components = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet];
I have a string that I like to trim the first and last character. What is the equivalent of ltrim/rtrim in IOS?
This is the string:
"["email#email.com","email#email.com"]"
I want to remove only the first and last double quotes.
substringWithRange:
string = [string substringWithRange:(NSRange){1, str.length - 2}];
NSString *str = #" sample string ";
NSString *trimmedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
I've been trying to get rid of the white spaces in an NSString, but none of the methods I've tried worked.
I have "this is a test" and I want to get "thisisatest".
I've used whitespaceCharacterSet, which is supposed to eliminate the white spaces.
NSString *search = [searchbar.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
but I kept getting the same string with spaces. Any ideas?
stringByTrimmingCharactersInSet only removes characters from the beginning and the end of the string, not the ones in the middle.
1) If you need to remove only a given character (say the space character) from your string, use:
[yourString stringByReplacingOccurrencesOfString:#" " withString:#""]
2) If you really need to remove a set of characters (namely not only the space character, but any whitespace character like space, tab, unbreakable space, etc), you could split your string using the whitespaceCharacterSet then joining the words again in one string:
NSArray* words = [yourString componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* nospacestring = [words componentsJoinedByString:#""];
Note that this last solution has the advantage of handling every whitespace character and not only spaces, but is a bit less efficient that the stringByReplacingOccurrencesOfString:withString:. So if you really only need to remove the space character and are sure you won't have any other whitespace character than the plain space char, use the first method.
I prefer using regex like this:
NSString *myString = #"this is a test";
NSString *myNewString = [myString stringByReplacingOccurrencesOfString:#"\\s"
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, [myStringlength])];
//myNewString will be #"thisisatest"
You can make yourself a category on NSString to make life even easier:
- (NSString *) removeAllWhitespace
{
return [self stringByReplacingOccurrencesOfString:#"\\s" withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, [self length])];
}
Here is a unit test method on it too:
- (void) testRemoveAllWhitespace
{
NSString *testResult = nil;
NSArray *testStringsArray = #[#""
,#" "
,#" basicTest "
,#" another Test \n"
,#"a b c d e f g"
,#"\n\tA\t\t \t \nB \f C \t ,d,\ve F\r\r\r"
,#" landscape, portrait, ,,,up_side-down ;asdf; lkjfasdf0qi4jr0213 ua;;;;af!####$$ %^^ & * * ()+ + "
];
NSArray *expectedResultsArray = #[#""
,#""
,#"basicTest"
,#"anotherTest"
,#"abcdefg"
,#"ABC,d,eF"
,#"landscape,portrait,,,,up_side-down;asdf;lkjfasdf0qi4jr0213ua;;;;af!####$$%^^&**()++"
];
for (int i=0; i < [testStringsArray count]; i++)
{
testResult = [testStringsArray[i] removeAllWhitespace];
STAssertTrue([testResult isEqualToString:expectedResultsArray[i]], #"Expected: \"%#\" to become: \"%#\", but result was \"%#\"",
testStringsArray[i], expectedResultsArray[i], testResult);
}
}
Easy task using stringByReplacingOccurrencesOfString
NSString *search = [searchbar.text stringByReplacingOccurrencesOfString:#" " withString:#""];
This may help you if you are experiencing \u00a0 in stead of (whitespace). I had this problem when I was trying to extract Device Contact Phone Numbers. I needed to modify the phoneNumber string so it has no whitespace in it.
NSString* yourString = [yourString stringByReplacingOccurrencesOfString:#"\u00a0" withString:#""];
When yourString was the current phone number.
stringByReplacingOccurrencesOfString will replace all white space with in the string non only the starting and end
Use
[YourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
- (NSString *)removeWhitespaces {
return [[self componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]]
componentsJoinedByString:#""];
}
This for me is the best way SWIFT
let myString = " ciao \n ciao "
var finalString = myString as NSString
for character in myString{
if character == " "{
finalString = finalString.stringByReplacingOccurrencesOfString(" ", withString: "")
}else{
finalString = finalString.stringByReplacingOccurrencesOfString("\n", withString: "")
}
}
println(finalString)
and the result is : ciaociao
But the trick is this!
extension String {
var NoWhiteSpace : String {
var miaStringa = self as NSString
if miaStringa.containsString(" "){
miaStringa = miaStringa.stringByReplacingOccurrencesOfString(" ", withString: "")
}
return miaStringa as String
}
}
let myString = "Ciao Ciao Ciao".NoWhiteSpace //CiaoCiaoCiao
That is for removing any space that is when you getting text from any text field but if you want to remove space between string you can use
xyz =[xyz.text stringByReplacingOccurrencesOfString:#" " withString:#""];
It will replace empty space with no space and empty field is taken care of by below method:
searchbar.text=[searchbar.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
Use below marco and remove the space.
#define TRIMWHITESPACE(string) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
in other file call TRIM :
NSString *strEmail;
strEmail = TRIM(#" this is the test.");
May it will help you...
I strongly suggest placing this somewhere in your project:
extension String {
func trim() -> String {
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
func trim(withSet: NSCharacterSet) -> String {
return self.stringByTrimmingCharactersInSet(withSet)
}
}
pStrTemp = [pStrTemp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];