开发者

Do C# strings end with empty string?

开发者 https://www.devze.com 2023-02-03 22:53 出处:网络
Just a short question out of curiosity. string str = \"string\"; Console.WriteLine(str.EndsWith(string.Empty));//true

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 :)


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.


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 ;)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号