c#用两种语言分割和反转句子

时间:2012-03-08 18:12:01

标签: c# string reverse

我有这样的句子(希伯来语,RTL):

ואמר在这里אוראל

现在,当我将其插入数组时,我得到了:

  1. array [0] =אוראל
  2. array [1] = was
  3. array [2] = here
  4. array [3] =:ואמר
  5. 现在,因为希伯来语是一种RTL语言,我需要翻转英文字母,我需要切换位置。 请注意,我需要处理任何句子(这意味着我可以有两个以上的英语单词)。

    如何让结果如下所示?

    ואמרereh看到了אוראל

    我想也许可以用英语单词构建一个新字符串,反转它并再次构建原始字符串,或者可以在ref字符串中使用...


    感谢您的帮助,但我仍然遇到了问题! 正如你所说,我把句子分开,我颠倒了阵列 我明白了:

    לארואereh看到了רמאו

    在第2步之后,希伯来语的位置已经过了! 在第3步中,我再次颠倒了希伯来语,我得到了:

    אוראלereh看到ואמר

    我需要改变他们的位置(一对一,正如我所说的那样 有一个有很多单词的句子..) 所以,我不明白我是如何“将数组串重新组合在一起”(步骤5)

2 个答案:

答案 0 :(得分:5)

好吧,如果你将问题分成几部分,它就会像这样。你需要:

  1. Split a sentence into an array of strings
  2. reverse the Array
  3. Detect if a word is in Hebrew
  4. 如果不是希伯来语,则需要Reverse字符串
  5. 现在你需要在一个字符串中put the array of strings back together,你已经完成了!
  6. 编辑: 我误解了你的问题。你可以这样做:

        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);
        }
    

答案 1 :(得分:0)

保罗完成了算法的艰苦工作:

  
      
  1. 将句子拆分为字符串数组
  2.   
  3. 反转数组
  4.   
  5. 如果一个单词不是希伯来语,那么
  6.   
  7. 加入字符串
  8.   

这是一个使用LINQ的更优雅的版本:

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