How to initialise an NSMutableString to avoid \n when parsing an xml? - xml-parsing

I have a little problem and i can not find how to solve it.
Actually, I parse an Xml file and when i pick up a value in a tag with the
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
I have as result : LABELLEDEXPIRYPG = "Janv 2015\n ";
As you can see there is "\n and some spaces behind "Janv 2015"
I initialise my NSMutableString simply : currentExpiry = [[NSMutableString alloc] init];
and i use apendString to set NSMutableString :
if ([currentElement isEqualToString:kExpiry]){
[currentExpiry appendString:string];
}
Please someone can help me to find a way to solve that ?
Thanks for all !

This one worked for me:
NSString *temp = [string stringByReplacingOccurrencesOfString:#"\n" withString:#"" options:0 range:NSMakeRange(0, 1)];
Or you can use this line: very useful in XML parser:
NSString *temp = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Related

how to remove the extra spaces in UILabel in IOS

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

How to split string in iphone programming

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];
}

Separate NSMutablestring with comma [duplicate]

This question already has answers here:
How to append a NSMutableString with a NSMutableString?
(4 answers)
Closed 9 years ago.
i'm trying to append two NSMutablestring, but i want to make the comma separation between them. Right now the code i use is this:
NSString *astring = #"This is a string";
teststring = [[NSMutableString alloc]init];
[teststring appendString:astring];
NSLog(#"%#",teststring);
the result is :This is a stringThis is a String
i try with:
[teststring appendFormat:#",%#",astring];
but still no luck.
Please help, regards.
teststring = [[NSMutableString alloc]init];
[teststring appendString:#","];
[teststring appendString:astring];
Try this
NSString *astring = #"This is a string";
teststring = [[NSMutableString alloc]init];
[teststring appendString:astring];
[teststring appendString:#","];
[teststring appendString:astring];
NSLog(#"%#",teststring);
Try this
NSString *string = #"Hello World";
NSMutableString * secondstring =[[NSMutableString alloc]init];
[secondstring appendString:string]
[secondstring appendString:#","];
[secondstring appendString:string];
If you want to see this String
NSLog(#"%#",secondstring);
I used this and its working fine for me :
NSMutableString *teststring = [[NSMutableString alloc]init];
NSMutableString *astring = [[NSMutableString alloc]init];
[astring appendString:#"I am appending this string"];
[teststring appendFormat:#",%#",astring];
NSLog(#"string : %#",teststring);
O/P : string : ,I am appending this string

UILabel text with unicode NSString

This code shows correctly as Ecole with an accent on E:
NSString *test = #"\u00c9cole";
cell.detailTextLabel.text = test;
But when I get the string from my server sent as Json, I don't see E with an accent but rather the unicode \u00c9.
Code for getting Json string from server:
- (void) handleProfileDidDownload: (ASIHTTPRequest*) theRequest
{
NSMutableString *str = [[NSMutableString alloc] init];
[str setString:[theRequest responseString]];
[self preprocess:str]; //NSLog here shows str has the unicode characters \u00c9
}
- (void) preprocess: (NSMutableString*) str
{
[str replaceOccurrencesOfString:#"\n" withString:#"" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
[str replaceOccurrencesOfString:#"\"" withString:#"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [str length])];
[str replaceOccurrencesOfString:#"\\/" withString:#"/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [str length])];
}
Now if I do,
cell.detailTextLabel.text = str;
I don't get the accent for E rather \u00c9
What am I doing wrong?
NSString *test = #"\u00c9cole";
is converted by the complier to the accented E.
In your JSON, the string \u00c9cole is a literal backslash-u-zero-zero-c-nine.
You can get the same behavior by escaping the backslash.
NSString *test2 = #"\\u00c9cole";
This will give you the same bad result, \u00c9cole.
To correctly unescape the JSON string, see Using Objective C/Cocoa to unescape unicode characters, ie \u1234.
I'm providing the link instead of an answer because there are three decent answers. You can choose the best one for your needs.
NSLog here shows str has the unicode characters \u00c9
From that, you can know that the JSON you receive doesn't actually have the letter É inside but the escape sequence \u00c9. So, you need to somehow unescape this string:
CFMutableStringRef mutStr = (CFMutableStringRef)[str mutableCopy];
CFRange rng = { 0, [mutStr length] };
CFStringTransform(mutStr, &rng, CFSTR("Any-Hex/Java"), YES);
Then you can use mutStr in your code.

Incompatible pointer type error when using NSMutableString and stringByReplacingOccurrencesOfString

The purpose of the code shown below is to take a string from a textfield and remove commas and left parentheses, to prepare the contents for conversion to a float. For example, it takes the number 1,234,567 and changes it to 1234567.
The code snippet works, but returns an informational error "Incompatible pointer types assigning to NSMutableString * from String *"
When I use NSString instead of NSMutableString, I get no errors, but the returned value is an empty string.
Using the NSMutableString methodology, what is the problem and how can I change this to eliminate the 'Incompatible pointer type" error.
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"," withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#")" withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"(" withString:#"-"];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#" " withString:#""];
The stringByReplacingOccurrencesOfString is a method of NSString, and the return value is also a NSString. So you can't assign the return value to a NSMutableString variable.
Instead you can use the method replaceOccurrencesOfString:withString:options:range: from NSMutableString:
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:#"1,234,567"];
[cleanedDataCellTest replaceOccurrencesOfString:#"," withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:#")" withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:#"(" withString:#"-" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:#" " withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
But, for the consideration of performance, I think use NSString and its method stringByReplacingOccurrencesOfString is more efficient.
UPDATE: Sorry, yesterday I didn't test the "performance". I did a test just now, by replacing some thing in a string (about 26KB), use NSMutableString with replaceOccurrencesOfString:withString:options:range: is a little bit more efficient.
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
[cleanedDataCellTest replaceOccurrencesOfString:#"," withString:#"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
Ref: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablestring_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableString/replaceOccurrencesOfString:withString:options:range:
Switching to NSString worked for me...:
NSString *cleanedDataCellTest = [[NSString alloc] initWithString:#"1,234,098"];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"," withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#")" withString:#""];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#"(" withString:#"-"];
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"cleanedDataCellTest = %#", cleanedDataCellTest);
Shows:
cleanedDataCellTest = 1234098

Resources