Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

The following code works on "everyones" machine, except for mine:

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

I've tried in on .net 5.0.400, 5.0.403, 6.0.100 - all prints 'caa does not start with ca'. I've not copied the chars from anywhere, I literally type them out.

I understand that this is really just on my computer, but I do not understand why or what is causing this.

Edit: I'm on Windows 10 - OS Build: 19043.1348. nb-NO keyboard, en-US Windows UI language.

Edit: Added if (foo.StartsWith(bar, false, CultureInfo.GetCultureInfo("no") - no change in behavior.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda