I have NSString #" (\n "Bi_ss" \n) "
I want to get String #"Bi_ss "
Any one has any idea about this ?
Thank you
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
From Cocoanetics...
Take a look at the NSString method stringByReplacingOccurrencesOfString:withString for example, this should be able to perform the basic task you want it to do.
Related
I have a dynamic string suppose
NSString *originalString = #"Hello my Phone : 123123123 abc 987";
In the above string first i have to check were numbers are present inside the string which i got it by doing below code :-
NSString *newString = [[MessageStr componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
result i got in newString is 123123123987
But my problem is not getting out numbers from string, i just want to add a special character after every number inside the string which is dynamic.
like i want to make my string like this :-
NSString *originalString = #"Hello my Phone : 1,2,3,1,2,3,1,2,3 abc 9,8,7";
So, please any one can suggest me, what's best possible way to achieve above string.
Thanks in advance.
I finally succeed by doing the following code :-
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:#"([0-9])" options:0 error:NULL];
NSString *newString = [regexp stringByReplacingMatchesInString:MessageStr options:0 range:NSMakeRange(0, MessageStr.length) withTemplate:#"$0,"];
NSLog(#"Changed %#", newString);
Thanks #nsgulliver
by posting answer in the link :-
I'm doing this to encode my URL in this way,
but its not working,
i got the result in NSLog but its the same url nothing is changing.
Please help me to sort this issue.
below is my code :
NSString *unencodedUrlString =
[#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %#", unencodedUrlString);
Thanks in advance
The comma is a legal URL character, therefore stringByAddingPercentEscapesUsingEncoding leaves "2,7" as it is and does not replace it by "2%2C7".
If you want the comma to be replaced by a percent escape (as I understand from your
comment to the question), you can use CFURLCreateStringByAddingPercentEscapes
instead:
NSString *str = #"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7";
NSString *encoded = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef)(str), NULL, CFSTR(","), kCFStringEncodingUTF8));
NSLog(#"%#", encoded);
Output:
http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7
The fourth parameter CFSTR(",") specifies that the comma should be replaced by
a percent escape even if it is a legal URL character.
Use this
NSString *str = [NSString stringWithFormat:#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"];
NSString *path = [str stringByReplacingOccurrencesOfString:#"," withString:#"/"];
NSLog(#"%#",path);
This will do nothing but will make , to /.
i am getting one url from the server http: / /www.google.com, however due to space in between them it is not opening, please let me know how to remove the space in between them, so that it should open in safari.
To remove empty space:
str = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
Use following to detect site or phone number in text view.
textview.dataDetectorTypes = UIDataDetectorTypeAll;
NSString *cleanString = [dirtyString stringByReplacingOccurrencesOfString:#"http: //" withString:#"http://"];
I'd wonder why you get this odd string in the first place, though.
NSString *str = #"hel lo";
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
For remove empty spaces, please use below code,
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
I am new to Iphone development. I have tried the following code, but it does not work with middle whitespace in UITEXTFIELD.
NSString *t1 = [txt.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
How can I avoid middle whitespace?
Use this code may this use full.
NSString *t1=[txt.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *t1= [txt.text stringByReplacingOccurrencesOfString:#" " withString:#""]
Is there any function to remove the white spaces from text message in objective c?
For eg:for "How are you",the result should be "howareyou"
Thanks in advance.
You could use NSString's componentsSeparatedByCharactersInSet with whitespaceCharacterSet to first split the string on the whitespace, and then join the components using NSArray's componentsJoinedByString.
eg.
NSString *myString=#"How are you";
myString = [[myString componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: #""];
NSLog(myString); // displays Howareyou
Tom's approach isn't very efficient. What you want is:
-stringByTrimmingCharactersInSet:
NSString *myString = #"How are you";
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];