I'm receiving a JSON response and I need to escape quotation marks.
This is The string I get:
"sign":0,"text":"Continue onto William Elton \"Brownie\" Brown Freeway, I 580"
Now, I need to get the string looking like this:
\"sign\":0,\"text\":\"Continue onto William Elton \"Brownie\" Brown Freeway, I 580\"
But, when I call this method
sectionString = [sectionString stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
What I get is:
\"sign\":0,\"text\":\"Continue onto William Elton **\\"Brownie\\"** Brown Freeway, I 580\"
So the problem is with the bold part, Brownie, that already had quotation marks, before I started replacing them. How can I solve this, and only escape " and not \" as well?
To do what you asked for, first un-escape the escaped quotes:
Use stringByReplacingOccurrencesOfString to change all the \" into "
sectionString = [sectionString stringByReplacingOccurrencesOfString:#"\\\"" withString:#"\""];
Then use stringByReplacingOccurrencesOfString to change all the " into \"
sectionString = [sectionString stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
I am trying to get the text from TextField and convert to the char like the following:
const char *TEXT;
TEXT = [[_data text] UTF8String];
NSLog(#"TEXT = %s" , TEXT);
NSLog(#"strlen(TEXT) = %lu" , strlen(TEXT));
When I input the abcde in textfield , and the log show
TEXT = abcde
strlen(TEXT) = 5
If I want to send TEXT by TCP , should I add the '\0' by myself ? of it already has '\0' at the end ?
It contains, you don't need to add a null terminator (\0).
As it says in the documenation:
UTF8String
A null-terminated UTF8 representation of the string. (read-only)
So it's already null terminated.
the strlen function counts the length of the contents, not including the null at the end. The actual length of the data in your TEXT variable is one byte longer (for the null)
BTW, you should follow Cocoa naming conventions. Variables (and method names) should be lower case, with words after the first word capitalized ("camel case", as it's sometimes called, e.g. "capitalizedLikeThis")
Class names should start with an upper case character and then follow the same camel case rules. (e.g. "ThisIsAClass")
I am reading a line of code in from a source file on disk and the line is a string, and it is of a string that contains HTML code in it:
line = #"format = #"<td width=\"%#\">";"
I need to remove the escaped characters from the html string. So any place that there is a '\"', I need to replace it with ''. I tried this:
[line stringByReplacingOccurrencesOfString:#"\\""" withString:#""];
But it only removed the '\' character, not the accompanying '"'. How can I remove the escaped '"' from this string?
EDIT: The key part of this problem is that I need to figure out a way to identify the location of the first #", and the closing " of the string declaration, and ignore/remove everything else. If there is a better way to accomplish this I am all ears.
[s stringByReplacingOccurrencesOfString:#"\\\"" withString:#""]
The replacement string there is a slash, which has to be escaped in the literal replacement string using another slash, followed by a quote, which also has to be escaped in the literal by a slash.
Try use this:
NSString *unfilteredString = #"!##$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:#""];
NSLog (#"Result: %#", resultString);
I sending a byte array over a REST service. It is being received as String. Here is an extract of it. with start and end tags.
[0,0,0,0,32,122,26,65,0,0,0,0,96,123,26,65,0,0,0,0,192,123,20,65,0,0,0,0,0,125,20,65,71,73,70,56,57,97,244,1,244,1,247,0,0,51,85,51,51,85,102,51,85,153,51,85,204,51,85,255,51,128,0,51,128,51,51,128,102,51,128,153,51,128,204,51,128,255,51,170,0,51,170,51,51,170,102,51,170,153,51,170,204,51,170,255,51,213,0,51,213,51,51,213,102,51,213,153,51,213,204,51,213,255,51,255,0,51,255,51,51,255,102,51,255,153,51,255,204,51]
Now before anyone suggests sending it as a base64 encoded String, that would require Blackberry to actually have a working Base64 decoder. But alas, it fails for files over 64k and Ive tried alsorts.
Anyway this is what ive tried:
str = str.replace('[', ' ');
str = str.replace(']', ' ');
String[] tokens = split(str,",");
byte[] decoded = new byte[tokens.length];
for(int i = 0; i < tokens.length; i++)
{
decoded[i] = (byte)Integer.parseInt(tokens[i]);
}
But it fails. Where split is like the JAVA implementation found here.
Logically it should work? but its not. This is for JavaME / Blackberry. No Java Answers please (unless they work on javaME).
Two problems one minor and one that is a pain.
Minor:whitespaces (as mentioned by Nikita)
Major:casting to bytes ... since java only has unsigned byte, 128 and higher will become negative numbers when casting from int to byte.
str = str.replace('[',' ');
str = str.replace(']', ' ');
String[] tokens = split(str,",");//String[] tokens = str.split(",");
byte[] decoded = new byte[tokens.length];
for (int i = 0; i < tokens.length; i++) {
decoded[i] = (byte) (Integer.parseInt(tokens[i].trim()) & 0xFF);
}
for(byte b:decoded) {
int tmp = ((int)b) & 0xff;
System.out.print("byte:"+tmp);
}
(btw:implementing base64 encoder/decoder isn't especially hard - might be "overkill" for your project though)
Replace brackets with empty strings, not with spaces:
str = str.replace('[', '');
str = str.replace(']', '');
In your case you have following array:
[" 0", "0", "0", ..., "204", "51 "]
First element " 0" cannot be parsed to Integer.
I recommend to use Base64 encoded string to send byte array.
There's a post with link to Base64 library for J2ME.
This way allows you convert byte array to a string and later you can convert this string to byte array.