0

In the following code:

static void Main(string[] args)
{
    string MultiLineString = @"This is a
random sentence";
    int index=0;

    string test = "";

    Console.WriteLine(MultiLineString[9].ToString()); //it should print 'r' but it prints a white space

    for (int i = 0; i < MultiLineString.Length; i++)
    {
        if (MultiLineString[i] == 'r')
            index = i;
    }
    Console.WriteLine(index); // 11 is the index of 'r' in "random"

    foreach (char ch in MultiLineString)
        if (ch == ' ')
            test += "_";
        else
            test += ch;

    Console.WriteLine(test);
    // the output is:
    // This_is_a
    //random_sentece

}

I'm having hard time trying to realyze what is happening in the 9-10 indexs. At first I thought it was a space the was somehow created when I skiped a line but then it was not included in the test string.

Thanks in advance.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

3 Answers3

3
MultiLineString[0] -> 'T'
MultiLineString[1] -> 'h'
MultiLineString[2] -> 'i'
MultiLineString[3] -> 's'
MultiLineString[4] -> ' '
MultiLineString[5] -> 'i'
MultiLineString[6] -> 's'
MultiLineString[7] -> ' '
MultiLineString[8] -> 'a'
MultiLineString[9] -> '\r'
MultiLineString[10] -> '\n'
MultiLineString[11] -> 'r'

Depending on your environment, a newline can be either "\r\n", "\r", or "\n". For most Windows environments, a newline is typically represented as "\r\n" (two characters).

You can see the ASCII values for the characters in a string (instead of just their visual representations) by doing this:

for(int i = 0; i < MultiLineString.Length; i++)
{
    Console.WriteLine("{0} - {1}", i, (int)MultLineString[i]);
}

\r would be 13 and \n would be 10.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • But MultiLineString[11] is r, what is MultiLineString [10] ? – Daniel Gelman Jan 26 '13 at 18:56
  • @DanielGelman It looks like the verbatim string simply reflects the character(s) present in the physical source file. Try this in Visual Studio: **"File" - "Advanced Save Options..." - "Line endings" change to e.g. "Unix (LF)" - click "OK"**. Then save your file, recompile and run again. You now have only `"\n"`, not `"\r\n"`. So the file can hold any sequence, not just the one from `Environment.NewLine`. – Jeppe Stig Nielsen Jan 26 '13 at 19:29
  • +1. you are absolutely right - verbatim strings must include characters as they represented in source (see specification linked in my answer) and C# does not specify any new line normalization - as result you get new lines as they are stored in the file. – Alexei Levenkov Jan 26 '13 at 20:05
0

You explicitly included new line into your string ("\n\r") when declared string to be multiple line literal. The new line characters are located at positions 9 and 10 correspondingly.

"Verbatim string literal" covered in C# language specification - string literals as well on SO: Multiline String Literal in C# .

When you search for ' ' which is character with code 32, unlike '\n' (10) and '\r' (13) you only find real space, not new line "white space" characters.

Note that there are many other "space-like" characters so if you need to perform some particular handling of them - check out methods of Char structure like Char.IsWhiteSpace

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

As the others have said, you aren't taking into account the newline escape sequence, \n, and as such, your index is off by 1.

Take for example, this code:

using System;

public class Test
{
    public static void Main()
    {
        string test = @"T
e
s
t";
        for (int i =0 ; i < test.Length; i++)
        {
            Console.WriteLine("{0} == \\n? {1}", test[i], test[i] == '\n');
        }
    }
}

The output is:

T == \n? False

== \n? True
e == \n? False

== \n? True
s == \n? False

== \n? True
t == \n? False

As you can see, every other character is a newline character.

Lander
  • 3,369
  • 2
  • 37
  • 53