Search code examples
c#string.format

How to have numeric digits in a string to not convert to another language (eg Arabic)


I have code thus..

string text1 = "more text";
string text2 = string.Format("some text plus {0}", text1);

I convert it to Arabic:

string text1 = "المزيد من النص";
string text2 = string.Format("بعض النص بالإضافة إلى {0}", text1);

So far no problems!

I then switch my computer's language from English to Arabic and look at the code and the numbers have been automatically update to Arabic digits...

string text1 = "المزيد من النص";
string text2 = string.Format("{.} بعض النصوص بالإضافة إلى", text1);

(I had to fudge this using a period, but the Arabic symbol for 0 looks similar enough for this example).

Trouble is, when the Windows selected language is in Arabic, I am getting a string format exception error because the Format command is not recognising the Arabic symbol for zero.

To be specific, my question is: How do you ensure that the zero entered remains as a Latin digit, even when in a non-Latin language? or How can I get this to work without erroring?

EDIT:

Cut and paste straight from the IDE : first one Windows set to English, second picture, same code, Arabic...

English

Arabic


Solution

  • String.Format(CultureInfo.InvariantCulture,"",) Should be the way to go, but you need to have the string split into 2 parts : The First Part contains the Arabic text or the English text. The second part should contain the Numeric part, and that is the one you should be formatting.

    Here is the documentation from Microsoft for you to look at and determine the specific culture that you want to use. https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0#System_String_Format_System_IFormatProvider_System_String_System_Object_