represents a character as a utf-16 code unit

Siva S picture Siva S · Aug 3, 2016 · Viewed 12.8k times · Source
StringBuilder builder = new StringBuilder();
builder.Append(" \(Nested \(parentheses\) dont need a backslash.\) \(But a single \\(parenthe)");
for(int i=0;i<builder.Length;i++)
{
    if(data[i] == "\" && data[i-1] == "\")
        data[i] += " ";
}

If two backslash is there in a string. I need to add a space in between them. In above code I get error "represents a character as a utf-16 code unit" on if condition. Because, using this, I am creating a PDF file. Thanks in advance.

Answer

Mong Zhu picture Mong Zhu · Aug 3, 2016

The message you get when hovering with the mouse over this piece of code: "\" in this line:

 if (data[i] == "\" && data[i-1] == "\")

is actually not an error but the comment/description of the class String.

A backslash is used to denote an escape sequence. The documentation tells:

Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.

The confusion arises because the compiler thinks you want to have the " as a character in your string when you use \". This is why intellisense still marks the rest of the line in the color of a string until the point where it finds a " without a backslash in front of it.

You can use the @ operator to force the string to be interpreted as it is, so a backslash will become just a character:

if (data[i] == @"\" && data[i-1] == @"\")

and your code becomes compilable.

Unfortunately your loop would break in the first iteration when i is 0. Because data will not be indexable with -1. I would suggest a different approach if your problem is to put a space between two consecutive backslashes.

You could use simply the Replace method like this:

string s =  @"\(Nested \(parentheses\) dont need a backslash.\) \(But a single \\(parenthe)";

s = s.Replace(@"\\", @"\ \");

I don't quite understand which role the data array plays in your code but you can also do it with the StringBuilder

StringBuilder builder = new StringBuilder();
builder.Append(@" \(Nested \(parentheses\) dont need a backslash.\) \(But a single \\(parenthe)");

builder.Replace(@"\\", @"\ \");