It's easy so declare a raw string
String raw = @'\"Hello\"';
But how can I convert a existing string to a raw string ?
This can come with file reading or ajax call : I want read the file as a raw string but the readAsText
method give me a no raw string.
I tryied thing like :
String notRaw = '\"Hello\"';
String raw = @raw;
But not compiling.
How can I do this?
EDIT : My need is to read the string char by char. So I don't want to read \" as one char " but as two chars \ and "
If you want to read a file without interpreting escape characters, then you need to readAsBytes
, which will give you a list of characters as integers. You can then detect a backslash and quote as:
final int backSlash = @'\'.charCodeAt(0);
final int quote = @'"'.charCodeAt(0);
You then pass the desired substrings to a string constructor:
String goodString = new String.fromCharCodes(byteList.getRange(start, end));
A raw string literal is still a string. The only difference is that it everything within a raw literal is accepted as-is, i.e. no escape sequences.
For example, in a plain (non-raw) string literal 'a\nb'
represents letter 'a', newline, letter 'b'; in a raw string literal it represents letter 'a', backslash, letter 'n', letter 'b'.