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?
Related
I'm new in iPhone development.I'm developing one chatting application.i was facing problem while sending and receiving some special character.finally, i succeeded to post and get this special character.now,there is only one special character which i'm not able to get it.while i'm sending % to the web-service,i get %25.
At sending string to web-serive use bellow string..
NSString *string = #"Hello % How Are You?";
string = [string stringByReplacingOccurrencesOfString:#"%" withString:#"%25"];
NSLog(#"\n\n ===>> Before ==>> %#",string);
NSString *string2 = string;
string2 = [string2 stringByReplacingOccurrencesOfString:#"%25" withString:#"%"];
NSLog(#"\n\n ===>> After ==>> %#",string2);
OutPut =>
===>> Before ==>> Hello%2520%2525%2520how%2520are%2520you?
===>> After ==>> Hello %2526 How Are You?
try this code...
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:#"&"];
string2 = [string2 stringByReplacingOccurrencesOfString:#"%25" withString:#"%"];
NSLog(#"\n\n ===>> After ==>> %#",string2);
OUTPUT IS:
===>> Before ==>> Hello%20%25%20How%20Are%20You?
====>> After ==>> Hello % How Are You?
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:#""];
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).
I'm developing an application for the iPhone that has inApp-mail sending capabilities. So far so good, but now I want to avoid html-injections as some parts of the mail are user-generated texts.
Basically I search for something like this:
// inits
NSString *sourceString = [NSString stringWithString:#"Hello world! Grüße dich Welt <-- This is in German."];
// ----- THAT'S WHAT I'M LOOKING FOR
// pseudo-code |
// V
NSString *htmlEncodedString = [sourceString htmlEncode];
// log
NSLog(#"source string: %#", sourceString);
NSLog(#"encoded string: %#", htmlEncodedString);
Expected output
source string: Hello world! Grüße dich Welt <-- This is in German.
encoded string: Hello world! Grüße dich Welt <-- This is in German.
I already googled and looked through several of SO's questions and answers, but all of them seem to be related to URL-encoding and that's not what I really need (I tried stringByAddingPercentEscapesUsingEncoding with no luck - it creates %C3%BC out of an 'ü' that should be an ü).
A code sample would be really great (correcting mine?)...
--
Thanks in advance,
Markus
Check out my NSString category for HTML. Here are the methods available:
- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
Thanks #all. I ended up using my own implementation:
//
// _________________________________________
//
// textToHtml
// _________________________________________
//
- (NSString*)textToHtml:(NSString*)htmlString {
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"<" withString:#"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#">" withString:#">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"""" withString:#"""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"'" withString:#"'"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"\n" withString:#"<br>"];
return htmlString;
}
A little improvement on #Markus' code [Change <br /> to <p></p>, escape multiple spaces]
- (NSString*)textToHtml:(NSString*)htmlString {
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"<" withString:#"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#">" withString:#">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"""" withString:#"""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"'" withString:#"'"];
htmlString = [#"<p>" stringByAppendingString:htmlString];
htmlString = [htmlString stringByAppendingString:#"</p>"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"\n" withString:#"</p><p>"];
// htmlString = [htmlString stringByReplacingOccurrencesOfString:#"\n" withString:#"<br />"];
while ([htmlString rangeOfString:#" "].length > 0) {
htmlString = [htmlString stringByReplacingOccurrencesOfString:#" " withString:#" "];
}
return htmlString;
}
I have been looking for a similar solution and this did the job for me
NSString* value = #"<&>";
const void* keys[1] = {CFSTR("somekey")};
const void* values[1] = {value};
CFDictionaryRef dicRef = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, nil, nil);
CFDataRef dataRef = CFPropertyListCreateData(kCFAllocatorDefault, dicRef, kCFPropertyListXMLFormat_v1_0, 0, NULL);
NSString *str = [[NSString alloc]initWithData:(NSData *)dataRef encoding:NSUTF8StringEncoding];
NSRange start =[str rangeOfString:#"string>"];
NSRange end =[str rangeOfString:#"</string"];
NSString *substr = [str substringWithRange:NSMakeRange(start.location+start.length, end.location-(start.location+start.length))];
[str release];
CFRelease(dicRef);
CFRelease(dataRef);
//Substring is now html entity encoded
I am using some of the features that is used when saving plist files. I hope this helps.
I'm expanding #Markus answer, because my case is i'm sending JSON string, so i need to added some escape, these are my function :
note :
the exception reference from w3schools. https://www.w3schools.com/tags/ref_urlencode.asp
- (NSString*)convertStringToHTMLEscape:(NSString*)stringContent
{
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"{" withString:#"%7B"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"}" withString:#"%7D"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"[" withString:#"%5B"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"]" withString:#"%5D"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"\"" withString:#"%22"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"\\" withString:#"%5C"];
stringContent = [stringContent stringByReplacingOccurrencesOfString:#"/" withString:#"%2F"];
return stringContent;
}
Assuming the character encoding of the email supports Unicode - say UTF-8 - could you not just find and replace the occurrences of <, >, and & with <, >, and &?