I have one NSMutableArrayin that array I am storing some strings which i got from web service response.in that array strings are:
"Test Test\n \n \n ",
"abc Abc\n \n \n "
"sss Pr\n \n \n ",
"Gggf anil L\n \n \n ",
I want to split strings from \n.now how can i separate strings from \n.
Please help me.Thanks in advance.
NSArray * components = [yourString componentsSeperatedByString:#"\n"];
for(NSString *str in components)
{
NSLog(#"%#",str);
}
I think you just need to trim \n NOT the split.
NSString *trimmedValue = [array[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
In this case separating string from \n is BAD programming practice and the process is too much laindy. So better go for trimming string by \n. Try this -
NSString *trimmedString = [yourArray[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
Hope this will Help you. Thank you in advance.
This action can be performed in 2 steps.
1 .
NSArray * seperatedArr1 = [yourString componentsSeperatedByString:#","];
2.
NSMutableArray *finalArr = [[NSMutableArray alloc]init]
for(NSString *str in seperatedArr1)
{
NSString *trimmedString = [finalArr[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
[finalArr addobject:trimmedString];
}
Related
I want to display text which return from server in a label. Server return something like this *English\nMalay\nTamil\n*.
Currently I use:
stringByReplacingOccurrencesOfString:#"\n" withString:#", "
to replace the \n with comma. But how do I remove the last \n?
Now my text in the label shows:
"English, Malay, Tamil,"
I would like to get like
"English, Malay, Tamil"
Try like this way.
NSString *string = #"English\nMalay\nTamil\n";
//Make array from string
NSMutableArray *array = [NSMutableArray arrayWithArray:[string componentsSeparatedByString:#"\n"]];
//Remove all empty object from array
[array removeObject:#""];
//Join array object and make string
NSString *newString = [array componentsJoinedByString:#", "];
NSLog(#"%#",newString);
Why not remove last \n before making replacement by using substringToIndex?
NSString *mString = #"English\nMalay\nTamil\n";
NSLog(#"mString===before===%#", mString);
mString = [mString substringToIndex:mString.length-1];
mString = [mString stringByReplacingOccurrencesOfString:#"\n" withString:#", "];
NSLog(#"mString===after===%#", mString);
Output
mString===before===English
Malay
Tamil
mString===after===English, Malay, Tamil
You can do this by using below code
NSString *string=#"English\nMalay\nTamil\n";
NSMutableArray *arryLanguagge = [[string componentsSeparatedByString: #"\n"] mutableCopy];
[arryLanguagge removeObject:#""];
NSString *strLanguaage=[arryLanguagge componentsJoinedByString:#","];
NSLog(#"%#",strLanguaage);
I need to split NSString to array by specific word.
I've tried to use [componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]]
But the split is performed by a single character, and I need a few chars.
Example:
NSString #"Hello great world";
Split key == #" great ";
result:
array[0] == #"Hello";
array[1] == #"world";
Try
NSString *str = #"Hello great world";
//you can use the bellow line to remove space
//str = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
// split key = #"great"
NSArray *arr = [str componentsSeparatedByString:#"great"];
Code:
NSString *string = #"Hello great world";
NSArray *stringArray = [string componentsSeparatedByString: #" great "];
NSLog(#"Array 0: %#" [stringArray objectAtIndex:0]);
NSLog(#"Array 1: %#" [stringArray objectAtIndex:1]);
The easiest way is the following:
NSString *string = #"Hello Great World";
NSArray *stringArray = [string componentsSeparatedByString: #" "];
This can help you.
I retrieve NSData from a server containing \n. When I convert it into an NSString, \n is not working. It prints "\n".
My code is like this:
NSString *st=[NSString stringWithFormat:#"%#",data];
Thanks
Try with this:
NSString * string = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding];
That may help you.
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#" "];
My app uses addresses. I am not sure how to create a regex that strips away an address that reads like these.
" , Paxton, TX"
" , Dallas TX"
Need the above to be stripped so they read like this.
"Paxton, TX"
"Dallas TX"
Use the NSString API stringByTrimmingCharactersInSet:
NSString *test = #" , Paxton, TX";
NSMutableCharacterSet *charset = [[NSCharacterSet whitespaceCharacterSet] mutableCopy];
[charset addCharactersInString:#","];
NSString *final = [test stringByTrimmingCharactersInSet:charset];
NSLog(#"final = [%#]",final);
You can also do this without using regular expressions
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#" ,"];
NSString *trimmed = [#" , Payton, TX" stringByTrimmingCharactersInSet:charSet];
NSLog(#"%#", trimmed); //prints "Payton, TX"
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