Just a short question out of curiosity.
string str = "string";
Console.WriteLine(str.EndsWith(string.Empty)); //true
Console.WriteLine(str.LastIndexOf(string.Empty) == str.Length); //false
//of course string are indexed from 0,
//just wrote if for fun to check whether empty string get some extra index
///somehow by a miracle:)
//finally
Console.WriteLine(str.LastIndexOf(string.Empty)
== str.LastIndexOf('g')); //true :)
Syntax. do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.
The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once.
Syntax. iteration-statement: do statement while ( expression ) ; The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once. The expression must have arithmetic or pointer type.
EndsWith
:
Determines whether the end of this string instance matches the specified string.
All strings will match ""
at the end... or any other part of the string. Why? Because conceptually, there are empty strings around every character.
"" + "abc" + "" == "abc" == "" + "a" + "" + "b" + "" + "c" + ""
Update:
About your last example - this is documented on LastIndexOf
:
If value is String.Empty, the return value is the last index position in this instance.
A related issue is the use of null as a string terminator - which happens in C and C++, but not C#.
From MSDN - String Class (System)
:
In the .NET Framework, a String object can include embedded null characters, which count as a part of the string's length. However, in some languages such as C and C++, a null character indicates the end of a string; it is not considered a part of the string and is not counted as part of the string's length.
Try this:
string str = "string";
Console.WriteLine(str.EndsWith(string.Empty)); //true
Console.WriteLine(str.LastIndexOf(string.Empty) == str.Length-1); // true
Console.ReadLine();
So yes as Oded said, they do always match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With