21

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 :)
asked Jan 10, 2011 at 13:56
3
  • What is your concept of string.Empty? What is it? Commented Jan 10, 2011 at 13:58
  • 7
    Everything contains nothing. Nothing is everywhere. Commented Jan 10, 2011 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? Commented Jan 10, 2011 at 14:40

4 Answers 4

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.

answered Jan 10, 2011 at 13:57

Comments

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.

answered Jan 10, 2011 at 13:58

6 Comments

ooh Good plus one for having a critical site on Q? code snippet
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
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:)
Which would make the string.Length = string.Length + 1 for every string :)
@Pabuc, why? can you explain it a bit further?
|
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 ;)

answered Jan 10, 2011 at 14:10

Comments

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."

answered Jan 10, 2011 at 14:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.