最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to Convert Non-English Characters to English Using JavaScript - Stack Overflow

programmeradmin1浏览0评论

I have a c# function which converts all non-english characters to proper characters for a given text. like as follows

public static string convertString(string phrase)
        {
            int maxLength = 100;
            string str = phrase.ToLower();
            int i = str.IndexOfAny( new char[] { 'ş','ç','ö','ğ','ü','ı'});
            //if any non-english charr exists,replace it with proper char
            if (i > -1)
            {
                StringBuilder outPut = new StringBuilder(str);
                outPut.Replace('ö', 'o');
                outPut.Replace('ç', 'c');
                outPut.Replace('ş', 's');
                outPut.Replace('ı', 'i');
                outPut.Replace('ğ', 'g');
                outPut.Replace('ü', 'u');
                str = outPut.ToString();
            }
            // if there are other invalid chars, convert them into blank spaces
            str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
            // convert multiple spaces and hyphens into one space       
            str = Regex.Replace(str, @"[\s-]+", " ").Trim();
            // cut and trim string
            str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
            // add hyphens
            str = Regex.Replace(str, @"\s", "-");    
            return str;
        }

but i should use same function on client side with javascript. is it possible to convert above function to js ?

I have a c# function which converts all non-english characters to proper characters for a given text. like as follows

public static string convertString(string phrase)
        {
            int maxLength = 100;
            string str = phrase.ToLower();
            int i = str.IndexOfAny( new char[] { 'ş','ç','ö','ğ','ü','ı'});
            //if any non-english charr exists,replace it with proper char
            if (i > -1)
            {
                StringBuilder outPut = new StringBuilder(str);
                outPut.Replace('ö', 'o');
                outPut.Replace('ç', 'c');
                outPut.Replace('ş', 's');
                outPut.Replace('ı', 'i');
                outPut.Replace('ğ', 'g');
                outPut.Replace('ü', 'u');
                str = outPut.ToString();
            }
            // if there are other invalid chars, convert them into blank spaces
            str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
            // convert multiple spaces and hyphens into one space       
            str = Regex.Replace(str, @"[\s-]+", " ").Trim();
            // cut and trim string
            str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
            // add hyphens
            str = Regex.Replace(str, @"\s", "-");    
            return str;
        }

but i should use same function on client side with javascript. is it possible to convert above function to js ?

Share Improve this question edited Jan 28, 2012 at 13:09 Raj 22.9k14 gold badges104 silver badges146 bronze badges asked Jan 6, 2011 at 18:35 Adam RightAdam Right 9856 gold badges17 silver badges36 bronze badges 3
  • A better title might be: How to Convert Non-English Characters to English Using JavaScript – Chris Shouts Commented Jan 6, 2011 at 19:19
  • yes Chris, you are right. i've changed it. – Adam Right Commented Jan 6, 2011 at 20:24
  • This is the top link on Google and the answer I was looking for was buried. Relevant question & answer: stackoverflow./a/70288180/20073186 – Nora Söderlund Commented Jan 22, 2023 at 13:49
Add a ment  | 

4 Answers 4

Reset to default 8

This should be what you are looking for - check the demo to test.

   function convertString(phrase)
{
    var maxLength = 100;

    var returnString = phrase.toLowerCase();
    //Convert Characters
    returnString = returnString.replace(/ö/g, 'o');
    returnString = returnString.replace(/ç/g, 'c');
    returnString = returnString.replace(/ş/g, 's');
    returnString = returnString.replace(/ı/g, 'i');
    returnString = returnString.replace(/ğ/g, 'g');
    returnString = returnString.replace(/ü/g, 'u');  

    // if there are other invalid chars, convert them into blank spaces
    returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
    // convert multiple spaces and hyphens into one space       
    returnString = returnString.replace(/[\s-]+/g, " ");
    // trims current string
    returnString = returnString.replace(/^\s+|\s+$/g,"");
    // cuts string (if too long)
    if(returnString.length > maxLength)
    returnString = returnString.substring(0,maxLength);
    // add hyphens
    returnString = returnString.replace(/\s/g, "-");  

    alert(returnString);
}

Current Demo

Edit: Updated the demo to add for testing of input.

function convertString(phrase)
{
 var maxLength = 100;
 var str = phrase.toLowerCase();
 var charMap = {
  'ö': 'o',
  'ç': 'c',
  'ş': 's',
  'ı': 'i',
  'ğ': 'g',
  'ü': 'u'
 };

 var rx = /(ö|ç|ş|ı|ğ|ü)/g;

 // if any non-english charr exists,replace it with proper char
 if (rx.test(str)) {
  str = str.replace(rx, function(m, key, index) {
   return charMap[key];
  });
 }

 // if there are other invalid chars, convert them into blank spaces
 str = str.replace(/[^a-z\d\s-]/gi, "");
 // convert multiple spaces and hyphens into one space       
 str = str.replace(/[\s-]+/g, " ");
 // trim string
 str.replace(/^\s+|\s+$/g, "");
 // cut string
 str = str.substring(0, str.length <= maxLength ? str.length : maxLength);
 // add hyphens
 str = str.replace(/\s/g, "-"); 

 return str;
}

It's certainly possible to convert it...

ToLower -> toLowerCase, Replace => replace, Length => length

You'd have to code up IndexOfAny, but that's no big deal. But here's my question - why bother to do it client side? Why not call back to the server and execute the code all in one place? I do a lot of stuff like this. Check out the following link:

http://aspalliance./1922

It explains a way to bind, client-side, to server-side methods.

Although this is an old question, this is a problem I face frequently. I therefore wrote a tutorial on how to solve it. It's located here: http://nicoschuele./Posts/75.html

The short answer is this: first, you need to treat all the diacritical characters within a function and then, using a dictionnary you build, you need to process all the language specific letters. For example, "à" is a diacritical character and "Ø" is a norwegian letter. My tutorial uses .NET to achieve this but the principle, even in javascript, is the same.

发布评论

评论列表(0)

  1. 暂无评论