Reputation: 543
I can't figure out why EndsWith is returning false.
I have the C# code:
string heading = "yakobusho";
bool test = (heading == "yakobusho");
bool back = heading.EndsWith("sho");
bool front = heading.StartsWith("yak");
bool other = "yakobusho".EndsWith("sho");
Debug.WriteLine("heading = " + heading);
Debug.WriteLine("test = " + test.ToString());
Debug.WriteLine("back = " + back.ToString());
Debug.WriteLine("front = " + front.ToString());
Debug.WriteLine("other = " + other.ToString());
The output is:
heading = yakobusho
test = True
back = False
front = True
other = True
What is going on with EndsWith?
Upvotes: 1
Views: 193
Reputation: 2180
In your EndsWith
argument there is a special character.
As you can see from this code:
class Program
{
static void Main(string[] args)
{
string heading = "yakobusho";
string yourText = "sho";
bool back = heading.EndsWith(yourText);
Debug.WriteLine("back = " + back.ToString());
Debug.WriteLine("yourText length = " + yourText.Length);
string newText = "sho";
bool backNew = heading.EndsWith(newText);
Debug.WriteLine("backNew = " + backNew.ToString());
Debug.WriteLine("newText length = " + newText.Length);
}
}
OUTPUT:
back = False
yourText length = 4
backNew = True
newText length = 3
The length of yourText
is 4 and so there is some hidden character in this string.
Hope this helps.
Upvotes: 0
Reputation: 131180
The "sho"
string in the third line starts with a zero length space. "sho".Length
returns 4 while ((int)"sho"[0])
returns 8203, the Unicode value of the zero length space.
You can type it in a string using its hex code, eg :
"\x200Bsho"
Annoyingly, that character isn't considered whitespace so it can't be removed with String.Trim()
.
Upvotes: 1
Reputation: 631
This contains an invisible character before the "sho" string:
bool back = heading.EndsWith("sho");
The corrected line:
bool back = heading.EndsWith("sho");
Upvotes: 4