18

If this is not a bug, can anyone then explain the reason behind this behavior? Indeed it seems that every odd number of letters will return false:

string test = "aaaaaaaaaaaaaaaaaaaa";
Console.WriteLine(test.StartsWith("aa"));
Console.WriteLine(test.StartsWith("aaa"));
Console.WriteLine(test.StartsWith("aaaa"));
Console.WriteLine(test.StartsWith("aaaaa"));
Console.WriteLine(test.StartsWith("aaaaaa"));
Console.WriteLine(test.StartsWith("aaaaaaa"));

yields following output when executed on a Danish system:

True
False
True
False
True
False
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
sondergard
  • 3,184
  • 1
  • 16
  • 25
  • 3
    I don't believe it can be true. For example, in Ideone, [not](http://ideone.com/F3S7vQ) Can be a `Culture` thing maybe.. – Soner Gönül Mar 21 '13 at 12:34
  • 1
    I have copied and pasted your code snippet to LINQPad - every line gave `true`. – Patryk Ćwiek Mar 21 '13 at 12:34
  • 1
    How are you doing this? I copied your code and it yields all 'True'. – Alexander Tsvetkov Mar 21 '13 at 12:35
  • This question is not so bad. Not everyone have the same culture! – Ken Kin Mar 21 '13 at 12:40
  • 2
    @Matthew Well surely you agree that the culture is a pretty crucial information here, and even with an appropriate culture, Mono does *not* reproduce this behaviour. But yeah, that was a bummer. – Konrad Rudolph Mar 21 '13 at 12:46
  • Matthew is right, I found it out myself right after I posted the question. In danish aa corresponds to "å" so that's why every odd number of a's returns false. Comparing by invariant culture fixes the problem :) – sondergard Mar 21 '13 at 12:46
  • Just another `Culture` issue.. http://ideone.com/vHT1rR – Soner Gönül Mar 21 '13 at 12:50
  • Sorry I removed my comment of earlier - yes, the culture is pretty important info BUT it would have been a good idea to ask about it before downvoting! The OP might not have thought about it. – Matthew Watson Mar 21 '13 at 12:51

1 Answers1

22

This is certainly due to your current culture. You may be in Danish in which aa is considered a letter. If you try changing the culture.. or the case, it shall work.

I think I remember similar behaviour with hungarian cultures and letter associations

Have a look to String StartsWith() issue with Danish text

Example:

using System;
using System.Globalization;

namespace Demo
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("da-DK");
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
            string test = "aaaaaaaaaaaaaaaaaaaa";
            Console.WriteLine(test.StartsWith("aa"));
            Console.WriteLine(test.StartsWith("aaa"));
            Console.WriteLine(test.StartsWith("aaaa"));
            Console.WriteLine(test.StartsWith("aaaaa"));
            Console.WriteLine(test.StartsWith("aaaaaa"));
            Console.WriteLine(test.StartsWith("aaaaaaa"));
        }
    }
}

This prints what the OP claims.

Community
  • 1
  • 1
Kek
  • 3,145
  • 2
  • 20
  • 26
  • 4
    Executing `Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("hu-hu");` before his code still results in all lines yielding `True`. – Daniel Hilgarth Mar 21 '13 at 12:39
  • My appologies.. It is danish. Try and set Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da-DK"); – Kek Mar 21 '13 at 12:42
  • But indeed, danish yields the expected - or rather *unexpected* - results of the OP: `Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da");` – Daniel Hilgarth Mar 21 '13 at 12:42
  • Still doesn’t work on Mono but that may be due to lacking locale support. However, can you explain more on *why* this behaviour is the way it is? I don’t understand it even though I have a passable grasp of Unicode, encoding and locales. – Konrad Rudolph Mar 21 '13 at 12:44
  • 3
    Indeed, in danish "aa" corresponds to the single letter "å", which is the reason every odd number of a's resulted in false. :) – sondergard Mar 21 '13 at 12:45