I have a string coming from web service like below
//coming string is like below format. As user commented unusually.For eg User pressed New lines three times,entered text and again pressed new line and posted the comment as below
Super
How to remove the new lines, spaces other stuff other than text in UILabel.I tried with below code but it is not trimmed..
NSString* result = [mystring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
custom.mylabel.text=result;
//also tried with below code
custom.mylabel.text=[mystring stringByReplacingOccurrencesOfString:#"\n" withString:#""];
custom.mylabel.text=[mystring stringByReplacingOccurrencesOfString:#"\t" withString:#""];
Please suggest any ideas how to fix it..
Thanks in Advance..
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
try like this
NSString * val = #"(555) 555-555 Off\nice \n";
NSString * strippedNumber = [val stringByReplacingOccurrencesOfString:#"[^a-z^A-Z]" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, [val length])];
NSLog(#"%#", strippedNumber);
O/P:-Office
Related
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:#"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:#"-" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:#"(" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#")" withString:#""];
txtRecipient.text = contactString;
}
This is my code. I am using contactPicker to pick a contact from phonebook. Then I am storing it in to a string variable. After that I am removing dashes, brackets and spaces from string value by using stringByReplacingOccurrencesOfString. Every thing is working fine except for this line:
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""];
After this line of code contactString remains the same i.e, spaces didn't get removed from the string. I also tried componentsSeparatedByString function but its returning only 1 character. i.e,
NSArray *components = [dateString componentsSeparatedByString:#" "];
components.length is returning 1.
Hope you understand my question. Is there any other way of removing spaces from a string? Any kind of help would be appreciable. Thanks.
Look like this is not a standard space.
Try this:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:#"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:#"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}
i have string value
NSString *str = #"12:15"
now how to convert it in to integer value??
i tryNSInteger i =[str integerValue];
but it's return only 12 and i want 1215.
Please suggest.
Thank you
A more generic approach would be to replace everything but the numeric values with an empty string like below:
NSString *str = #"12: a15xx";
str = [str stringByReplacingOccurrencesOfString:#"[^0-9]"
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, str.length)];
NSLog(#"%d", str.integerValue); // prints 1215
a simple answer would be
NSString *str=#"12:15";
str = [str stringByReplacingOccurrencesOfString:#":" withString:#""];
NSInteger i =[str integerValue];
NSArray* arr = [#"12:15" componentsSeparatedByString: #":"];
NSString* strValue = [NSString stringWithFormat:#"%#%#",[arr objectAtIndex:0],[arr objectAtIndex:1]];
int value=(int)strValue;
I have a string of phone number with format (123)-(456)-7890 but how can i convert to the following form 1234567890?
Try this
NSString *numberString = [[mixedString componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
It will give digits from a string.
stringByReplacingOccurrencesOfString supports also regular expression
NSString *phoneNumber = #"(123)-(456)-7890";
NSString *filteredPhoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"[ ()-]"
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, phoneNumber.length)];
NSLog(#"%#", filteredPhoneNumber);
Put all characters to be ignored between the brackets in the first parameter.
An alternative regex is #"[^\\d+]" which means ignore all non-digit characters
NSString *str = #"(123)-(456)-7890";
str = [str stringByReplacingOccurrencesOfString: #"(" withString:#""];
str = [str stringByReplacingOccurrencesOfString: #")" withString:#""];
str = [str stringByReplacingOccurrencesOfString: #"-" withString:#""];
NSLog(#"Output = %#",str);
Output = 1234567890
You also replacing more character in Single Line of coding using stringByReplacingOccurrencesOfString
NSString *YourString = #"(123)-(456)-7890";
YourString = [[[YourString stringByReplacingOccurrencesOfString:#"(" withString:#""] stringByReplacingOccurrencesOfString:#")" withString:#""] stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"YourString == > %#",YourString);
YourString == > 1234567890
Use this code,
NSString *valString = #"(123)456-7890";
NSString* phoneStr=[[[valString stringByReplacingOccurrencesOfString:#"(" withString:#""] stringByReplacingOccurrencesOfString:#")" withString:#""] stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"phone String %#",phoneStr); // finally you get phone String 1234567890
its working for me, hope its helpful
If you want your code to work outside the USA, use Google's telephony library. Formatting and extracting phone numbers is difficult. I wouldn't even try doing it myself by hand.
I need to strip away any new line characters from a string. I tried the stringByReplacingOccurencesOfString method below. What is the correct method to do this in Objective C?
[textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSString *textWithNewLinesRemoved = [textLine stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
stringByReplacingOccurrencesOfString does not modify textLine
NSString *strippedTextLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
Or
NSString *textLine = #"My cool text\n";
textLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
I'm trying to send emails to a list that I get from a server which is an array of emails whose output is in this format
(
"john#gmail.com\n",
"katebell#gmail.com\n"
"\nakhil#gmail.com",
"mary#gmail.com",
"timcorb\n#gmail.com
)
Now as you can see some emails have newline characters in between and those emails doesnt get sent. I'm trying to find an efficient way to filter out those newlines, my current approach is to loop through all emails and check for newline in each email and if newline exist replace it with a null string. Is there a better way to do this or should I just stick with that? Also Will my current approach cause any issues in any other scenarios?
One way you can try using NSRegularExpression like this below :-
NSArray *array=#[#"john#gmail.com\n",#"katebell#gmail.com\n",#"\nakhil#gmail.com",#"mary#gmail.com",#"timcorb\n#gmail.com"];
NSString *string =[array componentsJoinedByString: #","];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"\n" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#""];
NSLog(#"%#",modifiedString);
Output:-
john#gmail.com,katebell#gmail.com,akhil#gmail.com,mary#gmail.com,timcorb#gmail.com
try something like this
NSString *fileName = #"\ntest\n";
fileName = [fileName stringByReplacingOccurrencesOfString:#"\n" withString:#""];
eg.
NSString * str = #"timcorb\n#gmail.com";
str = [str stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSLog(#"%#",str);
it will Log 2014-01-10 01:01:00.256 demo[26220] timcorb#gmail.com
You can use the below code for replacing characters in a string.
NSString *email = #"\nakhi\nl#gmail.com";
NSString *actualEmail = [email stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSMutableArray* emailArray = [[NSMutableArray alloc] init];
for (int _index = 0; _index < [yourArray count]; _index++) {
[emailArray addobject:[[yourArray objectAtIndex:_index] stringByReplacingOccurrencesOfString:#"\n" withString:#""]];
}
This will give you your email array