I have a iPhone application which return me a string from webservice with escape characters like "\n" and "\". Now I want to add this string in nsdictionary. for that I do below
NSMutableArray *keyArray = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *valueArray = [[NSMutableArray alloc] initWithCapacity:1];
[valueArray addObject:strVerifiedReceipt];
[keyArray addObject:#"PAYMENT_RECEIPT"];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSString* jsonString = [jsonDictionary JSONRepresentation];
here jsonString return me strVerifiedReceipt with escape characters come from webservice like below
"PAYMENT_RECEIPT": "{\n\"receipt\":{\"original_purchase_date_pst\":\"2012-10-10 03:29:12 America/Los_Angeles\", \"unique_identifier\":\"977ce60f38d875d12d0f1d7fe583d1d5e61f99e8\", \"original_transaction_id\":\"1000000056917869\", \"bvrs\":\"2.0\", \"transaction_id\":\"1000000056917869\", \"quantity\":\"1\", \"product_id\":\"com.cornerstonehealthtechnologies.meanexus.Nexus010\", \"item_id\":\"544678366\", \"purchase_date_ms\":\"1349864952265\", \"purchase_date\":\"2012-10-10 10:29:12 Etc/GMT\", \"original_purchase_date\":\"2012-10-10 10:29:12 Etc/GMT\", \"purchase_date_pst\":\"2012-10-10 03:29:12 America/Los_Angeles\", \"bid\":\"com.cornerstonehealthtechnologies.meanexus\", \"original_purchase_date_ms\":\"1349864952265\"}, \"status\":0}",
Use stringByReplacingOccurrencesOfString:withString:
jsonString = [[[jsonString stringByReplacingOccurrencesOfString:#"\n"
withString:#""] stringByReplacingOccurrencesOfString:#"\" withString:#""]
stringByReplacingOccurrencesOfString:withString: will definitely work, but to remove the backslashes, make sure you put 2, because putting just one will just escape the quotation mark.
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\\" withString:#""];
I have an idea to remove backslash \ from your jsonString
jsonString = [NSString stringWithFormat:#"%s",[jsonString UTF8String]]
And it is working for me and it gives me a valid JSON string.
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\" withString:#""];
Related
I'm creating one chatting application.but,now I'm facing one problem while sending special character "&". I'm Using php web-services for sending and receiving the messages.while sending "&" character,and after that when some one received this character,the application is crashed.
here replace that character with its code like bellow...
when pass it in web-service at that time replace it like bellow..
yourString = [yourString stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
and then when you want to receive it at that time replace with special character..
yourString = [yourString stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
after display it in UITextView or UITextField
UPDATE:
if (string2 != nil) {
string1 = [NSString stringWithString:string2];
NSString *strTemp = [NSString stringWithString:string2];
NSLog(#"\n\n Temp String ==>> %#",strTemp);
}
UPDATE:
NSString *yourString = #"Hello & How Are You?";
yourString = [yourString stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
NSLog(#"\n\n String Here => %#",yourString);
At sending string to web-serive use bellow strign..
NSString *yourStringForWeb = #"Hello & How Are You?";
yourStringForWeb = [yourStringForWeb stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
and pass this yourStringForWeb to the web-service...
NSString *string = #"Hello & How Are You?";
string = [string stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
string = [string stringByReplacingOccurrencesOfString:#"&" withString:#"%26"];
NSLog(#"\n\n ===>> Before ==>> %#",string);
NSString *string2 = string;
string2 = [string2 stringByReplacingOccurrencesOfString:#"%20" withString:#" "];
string2 = [string2 stringByReplacingOccurrencesOfString:#"%26" withString:#"&"];
NSLog(#"\n\n ===>> After ==>> %#",string2);
OutPut =>
===>> Before ==>> Hello%20%26%20How%20Are%20You?
===>> After ==>> Hello & How Are You?
I have problem with string. The string shows: ~00000000:termometr2: +26.9 st.C and I want to use only this part: +26.9 st.C in my textfield.text.
Thanks
NSString *fullStr = #"00000000:termometr2: +26.9 st.C";
NSArray *parts = [fullStr componentsSeparatedByString:#" "];
textField.text =[NSString stringWithFormat:#"%#",[parts objectAtIndex:1]];
it might help you:
NSArray *_array = [yourString componentsSeparatedByString:#":"];
[myTextField setText:[_array lastObject]]; // or any other component you want
Just do it this way, using the method stringByReplacingOccurrencesOfString: withString::
NSString *originalString = #"~00000000:termometr2: +26.9 st.C";
NSString *filteredString = [originalString stringByReplacingOccurrencesOfString:#"~00000000:termometr2: " withString:#""];
NSString *col1 = [aBook.name stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
NSLog(#"column1: %#", col1); //output:- ABC+Company
This is my xml data output.
It comes with '+' mark. How can i decode this?
Could you please help me?
You mean get the parts that are seperated by the '+' sign? You can do that with:
NSArray *stringArray = [col1 componentsSeparatedByString:#"+"];
[stringArray objectAtIndex:0]; //ABC
[stringArray objectAtIndex:1]; //Company
If you want to remove/replace the '+' sign you can do this:
NSString* string = [col1 stringByReplacingOccurrencesOfString:#"+" withString:#""]; //last string can be empty or have a string value
Hope this helped you!
You can also replace the plus signs with spaces, if that’s what you’re after:
NSString *name = [col1 stringByReplacingOccurrencesOfString:#"+" withString:#" "];
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];
I have an NSString *str, having value #"I like Programming and gaming."
I have to remove "I" "like" & "and" from my string so it should look like as "Programming gaming"
How can I do this, any Idea?
NSString *newString = #"I like Programming and gaming.";
NSString *newString1 = [newString stringByReplacingOccurrencesOfString:#"I" withString:#""];
NSString *newString12 = [newString1 stringByReplacingOccurrencesOfString:#"like" withString:#""];
NSString *final = [newString12 stringByReplacingOccurrencesOfString:#"and" withString:#""];
Assigned to wrong string variable edited now it is fine
NSLog(#"%#",final);
output : Programming gaming
NSString * newString = [#"I like Programming and gaming." stringByReplacingOccurrencesOfString:#"I" withString:#""];
newString = [newString stringByReplacingOccurrencesOfString:#"like" withString:#""];
newString = [newString stringByReplacingOccurrencesOfString:#"and" withString:#""];
NSLog(#"%#", newString);
More efficient and maintainable than doing a bunch of stringByReplacing... calls in series:
NSSet* badWords = [NSSet setWithObjects:#"I", #"like", #"and", nil];
NSString* str = #"I like Programming and gaming.";
NSString* result = nil;
NSArray* parts = [str componentsSeparatedByString:#" "];
for (NSString* part in parts) {
if (! [badWords containsObject: part]) {
if (! result) {
//initialize result
result = part;
}
else {
//append to the result
result = [NSString stringWithFormat:#"%# %#", result, part];
}
}
}
It is an old question, but I'd like to show my solution:
NSArray* badWords = #[#"the", #"in", #"and", #"&",#"by"];
NSMutableString* mString = [NSMutableString stringWithString:str];
for (NSString* string in badWords) {
mString = [[mString stringByReplacingOccurrencesOfString:string withString:#""] mutableCopy];
}
return [NSString stringWithString:mString];
Make a mutable copy of your string (or initialize it as NSMutableString) and then use replaceOccurrencesOfString:withString:options:range: to replace a given string with #"" (empty string).