Why Does "Abcd".Startswith("") Return True

Why does abcd.StartsWith() return true?

Yes - because it does begin with the empty string. Indeed, the empty string logically occurs between every pair of characters.

Put it this way: what definition of "starts with" could you give that would preclude this? Here's a simple definition of "starts with" that doesn't:

"x starts with y if the first y.Length characters of x match those of y."

An alternative (equivalent) definition:

"x starts with y if x.Substring(0, y.Length).Equals(y)"

Why does string.StartsWith(\u2D2D) always return true?

I think I'll have a try.

From what I get, is that U+2D2D was added in Unicode v6.1 (source / source).

The .NET framework, or the native calls rather, support a lower version:

The culture-sensitive sorting and casing rules used in string
comparison depend on the version of the .NET Framework. In the .NET
Framework 4.5 running on the Windows 8 operating system, sorting,
casing, normalization, and Unicode character information conforms to
the Unicode 6.0 standard. On other operating systems, it conforms to
the Unicode 5.0 standard. (source)

Thus it is required to mark it as an ignorable character, which behaves just as if the character wasn't even there.

Character sets include ignorable characters, which are characters that
are not considered when performing a linguistic or culture-sensitive
comparison. (source)

Example:

var culture = new CultureInfo("en-US");
int result = culture.CompareInfo.Compare("", "\u2D2D", CompareOptions.None);
Assert.AreEqual(0, result);

string.StartsWith uses a similar implementation, but uses CompareInfo.IsPrefix(string, string, CompareOptions) instead.

Bug in C# - FooBar.StartsWith(string.Empty) is true

It's not that this == value returns true, but CompareInfo.IsPrefix is documented as returning true for String.Empty:

Every string starts and ends with an
empty substring (""); therefore, if
prefix is an empty string, this method
returns true.

Why does StartsWith return the wrong value only on my machine?

Repro:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("nb-NO");

var foo = "caa";
var bar = "ca";
if (foo.StartsWith(bar))
{
Console.WriteLine($"{foo} starts with {bar}");
}
else
{
Console.WriteLine($"{foo} does not start with {bar}");
}

Since .NET 5, string comparison is done using different string comparison libraries (ICU). In Norwegian, "aa" apparently isn't the same as "a" + "a". See Behavior changes when comparing strings on .NET 5+.

Compare (thanks @Charlieface):

  • https://dotnetfiddle.net/19rGWv: .NET 4.7.2: "caa starts with ca"
  • https://dotnetfiddle.net/PG7aTY: .NET 6: "caa does not start with ca"

You'll want an ordinal (character numeric value) comparison:

foo.StartsWith(bar, StringComparison.Ordinal)

Why do Strings start with a in Java?

"" is an empty string containing no characters. There is no "empty character", unless you mean a space or the null character, neither of which are empty strings.

You can think of a string as starting with an infinite number of empty strings, just like you can think of a number as starting with an infinite number of leading zeros without any change to the meaning.

1 = ...00001
"foo" = ... + "" + "" + "" + "foo"

Strings also end with an infinite number of empty strings (as do decimal numbers with zeros):

1 = 001.000000...
"foo" = "foo" + "" + "" + "" + ...

Javascript search an array for a value starting with a certain value

You can use the find() function which allows you to pass a custom function in parameter that will be tested on each value. This way you can use startsWith() on each value of the array as you intended to do.

Example:

const array1 = ['abc','xyz'];

function findStartWith(arg) {
return array1.find(value => {
return arg.startsWith(value);
});
}

console.log(findStartWith("hello")); // undefined
console.log(findStartWith("abcd")); // abc
console.log(findStartWith("xyzz")); // xyz


Related Topics



Leave a reply



Submit