none
在土耳其小语种下,字符串比较时忽略大小写,用StringComparison.CurrentCultureIgnoreCase比较结果不对 RRS feed

  • 问题

  • 当前线程切换到对应区域语言线程,字符串比较时忽略大小写用StringComparison.CurrentCultureIgnoreCase,在中文、英文泰语、越南语小语种下比较结果不对,在土耳其语言下比较结果不对,请专家帮忙解答一下我的问题?

    测试结果及测试代码如下:

    测试Equals忽略大小写方法

    语言ID:2052,语言名称:中文,调用Equals方法(CurrentCultureIgnoreCase)比较结果:True

    语言ID:1033,语言名称:英文,调用Equals方法(CurrentCultureIgnoreCase)比较结果:True
    语言ID:1055,语言名称:土耳其,调用Equals方法(CurrentCultureIgnoreCase)比较结果:False
    语言ID:1054,语言名称:泰语,调用Equals方法(CurrentCultureIgnoreCase)比较结果:True
    语言ID:1066,语言名称:越南语,调用Equals方法(CurrentCultureIgnoreCase)比较结果:True

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace TestLangApp
    {
        class Program
        {
    		static void Main(string[] args)
    		{
    			Console.WindowWidth = 120;
    			TestEqual();
    			Console.ReadLine();
    		}
    
    		static void TestEqual()
    		{
    			var souceStr = "join";
    			var destStr = "JOIN";
    			var langIdList = new List<int>();
    			langIdList.Add(2052);
    			langIdList.Add(1033);
    			langIdList.Add(1055);
    			langIdList.Add(1054);
    			langIdList.Add(1066);
    			Console.WriteLine("测试Equals忽略大小写方法");
    			foreach (var langId in langIdList)
    			{
    				Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(langId);
    				var lcId = Thread.CurrentThread.CurrentCulture.LCID;
    				var lcName = Thread.CurrentThread.CurrentCulture.NativeName;
    				var cultureStr = souceStr.Equals(destStr, StringComparison.CurrentCultureIgnoreCase);
    				Console.WriteLine(string.Format("语言ID:{0},语言名称:{1},调用Equals方法(CurrentCultureIgnoreCase)比较结果:{2}", lcId, GetLangName(Convert.ToInt32(lcId)), cultureStr));
    				//var ordinalStr = souceStr.Equals(destStr, StringComparison.OrdinalIgnoreCase);
    				//Console.WriteLine(string.Format("语言ID:{0},语言名称:{1},调用Equals方法(OrdinalIgnoreCase)比较结果:{2}", lcId, GetLangName(Convert.ToInt32(lcId)), ordinalStr));
    			}
    		}
    
    		static string GetLangName(int langId)
    		{
    			var langName = "";
    			switch (langId)
    			{
    				case 2052:
    					langName = "中文";
    					break;
    				case 1033:
    					langName = "中文";
    					break;
    				case 1055:
    					langName = "土耳其";
    					break;
    				case 1054:
    					langName = "泰语";
    					break;
    				case 1066:
    					langName = "越南语";
    					break;
    			}
    			return langName;
    		}
    	}
    }
    

    2022年3月8日 8:48

答案

全部回复