【问题标题】:c# split and reverse sentence with two languagesc#用两种语言拆分和反转句子
【发布时间】:2012-03-08 18:12:01
【问题描述】:

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

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

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

  1. 数组[0] = אוראל
  2. 数组[1] = 是
  3. 数组[2] = 这里
  4. 数组[3] = :ואמר

现在,因为希伯来语是一种 RTL 语言,我需要反转英文字母并且需要切换位置。 请注意,我需要注意任何句子(这意味着我可以有两个以上的英文单词)。

我怎样才能得到如下的结果?

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

我想可能随时用英文单词构建一个新字符串,将其反转并再次构建原始字符串,或者可能在引用字符串中使用......


感谢您的帮助,但我仍然遇到问题! 正如你所说,我已经拆分了句子,我颠倒了数组并 我明白了:

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

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

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

我需要转换他们的位置(一对一,我说过我可以 有很多单词的句子..) 所以,我不明白我如何“将数组字符串重新组合在一起”(第 5 步)

【问题讨论】:

  • 它是一种家庭作业吗 - 用“家庭作业”标签标记...也不清楚你有什么问题 - 你列出了几种方法但没有问题。

标签: c# string reverse


【解决方案1】:

好吧,如果你把问题分成几部分,它会是这样的。你需要:

  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 即可!

编辑: 我误解了你的问题。你可以这样做:

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

【讨论】:

  • 感谢您的帮助,但我还是遇到了问题!正如你所说,我已经拆分了句子,我颠倒了数组,我得到了这个: לארוא ereh 在第 2 步之后看到了 רמאו 希伯来语单词的位置已经磨损了!在第 3 步中,我再次颠倒了希伯来语单词,我得到了:אוראל ereh 看到了ואמר,我需要转换它们的位置(一对一,正如我所说,我可以用很多单词来造句..)所以,我不明白我如何“将数组字符串重新组合在一起”(第 5 步)帮助!
  • 抱歉,我误读了您的问题:我认为您需要将整个句子颠倒过来,只让英文单词从右到左。现在我明白希伯来语中的单词应该保持原位,而英语中的单词应该颠倒过来。我不明白在一般情况下这将如何工作。例如,假设 h1、h2 和 h3 是希伯来语中的单词,而 e1、e2、e3 是英语中的单词:你将如何处理像 h1、e1、e2、h2、h3、e3 这样的句子?它应该变成 h1, e2(backwards), e1(backwards), h2, h3, e3(backwards) 吗?还是h1、e3(向后)、e2(向后)、h2、h3、e1(向后)?
  • 是的,它将是:h1, e1, e2, h2, h3, e3 = h1, e2(backwards), e1(backwards), h2, h3, e3(backwards)
【解决方案2】:

Paolo 努力研究出算法:

  1. 将一个句子拆分成一个字符串数组
  2. 反转数组
  3. 如果某个词不是希伯来语,则将其反转
  4. 加入字符串

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

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

【讨论】:

  • 固定的“x”应该是“w”
猜你喜欢
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2013-04-07
  • 2017-11-14
  • 2021-09-24
  • 2020-08-11
  • 1970-01-01
相关资源
最近更新 更多