c# split and reverse sentence with two languages

我只是一个虾纸丫 提交于 2019-12-24 08:33:42

问题


I have a sentence like this (in Hebrew,RTL):

ואמר was here אוראל

Now, when I insert this into array I got:

  1. array[0] = אוראל
  2. array[1] = was
  3. array[2] = here
  4. array[3] = :ואמר

Now, because Hebrew is a RTL language I need to reverse the english letters and I need to switch the positions. Notice that I need to take care of any sentence (which means I can have more than two english words).

How do I get the result to be like the following?

ואמר ereh saw אוראל

I thought maybe to build any time a new string with just the english words,Reverse it and build the original string again, or maybe use in ref strings...


thanks for your helping but i still got a problem! i had splited the sentence as you said,i reversed the array and i got this:

לארוא ereh saw רמאו

after the step 2 the postions of the hebrew words are worng! in step 3 i reversed the hebrew words again and i got:

אוראל ereh saw ואמר

and i need to switch their position(one in one, as i said i can have a sentence with a lot of words..) so, i didn't understand how i "put the array string back together"(step 5)


回答1:


Paolo did the hard work of figuring out the algorithm:

  1. Split a sentence into an array of strings
  2. Reverse the Array
  3. If a word is not in Hebrew reverse it
  4. Join the strings

Here is a more elegant version using LINQ:

var result = Sentence
   .Split(' ')
   .Reverse()
   .Select(w => IsHebrew(w) ? w : new String(w.Reverse().ToArray())
   .Join(" ") 



回答2:


Well, if you split the problem into pieces, it would look like this. You need to:

  1. Split a sentence into an array of strings
  2. reverse the Array
  3. Detect if a word is in Hebrew
  4. if it is not in Hebrew you need to Reverse the string
  5. now you just need to put the array of strings back together in one string and you're done!

EDIT: I misunderstood your problem. You could do something like this:

    public static string ProcessEnglishHebrewSentence(string sentence)
    {
        var ret = new List<string>();
        string[] words = sentence.Split(' ');

        var curHebrewList = new List<string>();
        var curEnglishList = new List<string>();
        bool curLangIsHebrew=false;

        foreach(var w in words)
        {
            if(IsHebrew(w) && curLangIsHebrew) // we have a word in Hebrew and the last word was in Hebrew too
            {
                curHebrewList.Add(w);
            }
            else if(IsHebrew(w) && !curLangIsHebrew) // we have a word in Hebrew and the last word was in English
            {
                if(curEnglishList.Any())            {
                    curEnglishList.Reverse();
                    ret.AddRange(curEnglishList);
                } // reverse current list of English words and add to List
                curEnglishList = new List<string>(); // create a new empty list for the next series of English words
                curHebrewList.Add(w);
                curLangIsHebrew=true; // set current language to Hebrew
            }
            else if(!IsHebrew(w) && !curLangIsHebrew) // we have a word in English and the last word was in English
            {
                curEnglishList.Add(new String(w.Reverse().ToArray())); // reverse and add it to the current series of English words
            }
            else if(!IsHebrew(w) && curLangIsHebrew) // we have a word in English and the last word was in Hebrew
            {
                if(curHebrewList.Any()) ret.AddRange(curHebrewList); // add current list of Hebrew words to List of Lists
                curHebrewList = new List<string>(); // create a new empty list for the next series of Hebrew words
                curEnglishList.Add(new string(w.Reverse().ToArray()));
                curLangIsHebrew=false; // set current language to English
            }
            else
            {
                throw new Exception("there should be no other case...");
            }
        }
        if(curHebrewList.Any()) ret.AddRange(curHebrewList);
        if(curEnglishList.Any())
        {
            curEnglishList.Reverse();
            ret.AddRange(curEnglishList);
        }

        return ret.Aggregate((a,b) => a + " " + b);
    }


来源:https://stackoverflow.com/questions/9622610/c-sharp-split-and-reverse-sentence-with-two-languages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!