This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iphone sdk - Remove all characters except for numbers 0-9 from a string
i have string with a phone number as
mynumber = #"(334)687-6619";
I am using
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
to make a call.
But due to "(" , ")" my method is not able to make a call.
Please suggest me to make a right string
something like
"1-800-555-1212" or something like that.
Thanks
You can Use
NSString *s = #"(334)687-6619";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"("];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
s = [s stringByReplacingOccurrencesOfString:#")" withString:#"-"];
NSLog(#"%#", s);
So, It will replace "(" with "" & replace ")" with "-".
Thanks.
use this code
NSString *str = #"(334)687-6619";
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:#""];
str = [str stringByReplacingCharactersInRange:NSMakeRange(3, 1) withString:#"-"];
NSLog(#"%#",str);
OUTPUT is: 334-687-6619
To remove characters from a string:
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"()"];
NSArray *comps = [string componentsSeparatedByCharactersInSet:set];
NSString *newString = [comps componentsJoinedByString:#""];
Related
I have a string like:
http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg/
I want to use this string to load html page. But because of "/" at last in string, I get the URL is as null.
my code is as follow:
NSURL *ur=[NSURL URLWithString:Mystring];
NSLog(#"%#",ur); //getting null value
NSURLRequest *requestObj = [NSURLRequest requestWithURL:ur];
NSLog(#"%#",requestObj);
So, How can i remove this last "/" from string??
Are you sure it's because of the last '/'? Could it be because of the '~' character in the middle?
Whichever way, to check for the last character, simply do
unichar last = [urlString characterAtIndex:urlString.length - 1];
if (last == '/')
urlString = [urlString substringToIndex:urlString.length - 1];
try this code ...
NSString *Mystring = #"http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg/";
if ([Mystring isEqualToString:#""]||[Mystring isEqual:nil]) {
NSLog(#"\n\n String is NULL");
}
else {
NSString *TempURl = [Mystring substringToIndex:[Mystring length]-1];
NSString *urlstr =[TempURl stringByReplacingOccurrencesOfString:#" " withString:#""];
urlstr =[urlstr stringByReplacingOccurrencesOfString:#"\n" withString:#""];
urlstr =[urlstr stringByReplacingOccurrencesOfString:#"\t" withString:#""];
[urlstr retain];
NSURL *ur=[NSURL URLWithString:urlstr];
NSLog(#"%#",ur); //now get string value here
// NSURLRequest *requestObj = [NSURLRequest requestWithURL:ur];
//
// NSLog(#"%#",requestObj);
}
I Got Log OutPut : http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg
hope this help you...
If you are sure that you need to remove the last character from string in every case, then you can get a newString as
NSString *newString = [oldString substringToIndex:[oldString length]-1];
I think you need to encode the string like:
NSString *encodedString = [Mystring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
try this may help you
NSString * Mystring=#"http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg/";
[alfa substringToIndex:alfa.length-1];
This question already has answers here:
Split NSString multiple times on the same separator
(2 answers)
Closed 9 years ago.
I have a string "hai-welcome".
I need to split the above string using '-' separator:
mySstring = "hai-welcome"
Separate as:
firstString = "hai"
secondString = "welcome.
You can accomplish this by using "componentsSeparatedByString" method of NSString.
NSString * source = #"hai-welcome";
NSArray * stringArray = [source componentsSeparatedByString:#"-"];
NSString * firstPart = [stringArray objectAtIndex:0]; // Contains string "hai"
NSString * secondPart =[stringArray objectAtIndex:1]; //Contans string "welcome"
Use NSString's componentsSeparatedByCharactersInSet method
http://www.idev101.com/code/Objective-C/Strings/split.html
Try this
NSString *str = #"hai-welcome";
NSArray *listItems = [str componentsSeparatedByString:#"-"];
NSArray* foo = [#"hai-welcome" componentsSeparatedByString: #"-"];
NSString* first = [foo objectAtIndex: 0];
NSString* second = [foo objectAtIndex: 1];
If you wish to work with other than componentsSeparatedByString function, I would offer this solution to adopt:
NSString *myString = #"hai-welcome";
NSString *firstString = [myString substringWithRange:NSMakeRange(0, [myString rangeOfString:#"-"].location)];
NSString *secondString = [myString stringByReplacingOccurrencesOfString:[firstString stringByAppendingString:#"-"] withString:#""];
I have used this code for replacing "\n" with "", but it is not working.
How can I do it?
NSString *descString = [values objectForKey:#"description"];
descString = [descString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSLog(#"Description String ---->>> %#",descString);
catlog.description = descString;
[webview loadHTMLString:catlog.description baseURL:nil];
webview.opaque = NO;
try this
NSString *s = #"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"%#", s);
descString = [descString stringByReplacingOccurrencesOfString:#"\\n" withString:#""];
For more information refer to stringByReplacingOccurrencesOfString:withString: method
Hope this helps you.
UPDATE
Swift 3.0
descString.replacingOccurrences(of: "\\n", with: "");
and If I am not wring it will also work for Swift 4.0
Currently I am using stringByReplacingOccuranceOfString... to replace " with \", but is there any smarter way?
Thanks!
You can do like this,
NSString *str = #"hello";
NSString *escape = #"\\\””; // this adds ***//*** & then ***/"***
NSString *newStr = [NSString stringWithFormat:#"%#%#%#", escape, str, escape];
NSLog(#"%#", newStr);
Consider this code:
NSString *aString = #"\tThis is a sample string";
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"The trimmed string: %#",trimmedString);
trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"string"]];
NSLog(#"The trimmed string: %#",trimmedString);
Here if I use characterSetWithCharactersInString: on the same NSString object trimmedString, my previous whitespace trimming effect gets removed..
My question is,
Is there any possibility to use more than one NSCharacterSet object to the same NSString???
or Suggest me some other way to do this please, but the NSString object should be one and the same..
The problem is not because of character sets. Its because you are using aString while trimming the string second time. You should use trimmedString instead. Your code should look like,
trimmedString = [trimmedString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"string"]];
What about this:
NSString *aString = #"\tThis is a sample string";
NSMutableCharacterSet *customSet = [[NSMutableCharacterSet alloc] init];
[customSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[customSet addCharactersInString:#"string"];
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:customSet];
[customSet release];