20

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 :)
nan
  • 19,595
  • 7
  • 48
  • 80
  • What is your concept of `string.Empty`? What _is_ it? – thecoop Jan 10 '11 at 13:58
  • 7
    Everything contains nothing. Nothing is everywhere. – BoltClock Jan 10 '11 at 14:24
  • My question would be why are there so many up votes for such a meaningless question? Why exactly does it matter that one knows if a C# string does or does not end in an empty string? How exactly would you use such valuable information? – Blake7 Jan 10 '11 at 14:40

4 Answers4

18

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
6

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.

Pabuc
  • 5,528
  • 7
  • 37
  • 52
  • ooh Good plus one for having a critical site on Q? code snippet – Badr Jan 10 '11 at 14:02
  • I don't think OP's code `str.LastIndexOf(string.Empty)==str.Length` is a mistake though, as OP thinks `string.Empty` is something after **g** – Bolu Jan 10 '11 at 14:15
  • 1
    Of course, strings use 0-based indexing, I just wrote it for fun to check whether empty string get some extra index somehow by a miracle:) – nan Jan 10 '11 at 14:17
  • Which would make the string.Length = string.Length + 1 for every string :) – Pabuc Jan 10 '11 at 14:19
  • @Pabuc, why? can you explain it a bit further? – Bolu Jan 10 '11 at 14:21
  • Yes ofcourse I can Bolu. As Oded stated, For LastIndexOf, If the value is string.empty the return value is the last index position in this instance which is length-1 – Pabuc Jan 10 '11 at 14:27
2

Think about it this way: LastIndexOf is kind of meaningless with an empty string. You could say the empty string exists at every index within a string, between each character. The documentation thus provides a definitive answer for what should be returned:

If value is String.Empty, the return value is the last index position in this instance.

At least in this case it returns an actual index. If it returned the string's length (representing the index "after" the end, which I believe was your point) it would be returning a result for a method called LastIndexOf that isn't even an index.

And here's another way of looking at it: If I have this:

Dim index As Integer = str.LastIndexOf("")

...then I should be able to do this:

Dim substr As String = str.Substring(index, "".Length)

...and get "" back. Sure enough, when LastIndexOf returns the last index in the string, it works. If it returned the string's length, I'd get an ArgumentOutOfRangeException. Edit: Well, looks like I was wrong there. Hopefully my first point was strong enough on its own ;)

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
0

There's a some more information in this question and its answers.

In particular, "Indeed, the empty string logically occurs between every pair of characters."

Community
  • 1
  • 1
StuperUser
  • 10,555
  • 13
  • 78
  • 137