簡體   English   中英

C#兩種語言的拆分和反向句子

[英]c# split and reverse sentence with two languages

我有一個這樣的句子(希伯來語,RTL):

ואמר在這里אוראל

現在,當我將其插入數組時,我得到了:

  1. array [0] =אוראל
  2. array [1] =原為
  3. array [2] =這里
  4. array [3] =:ואמר

現在,因為希伯來語是RTL語言,所以我需要反轉英文字母,並且需要切換位置。 請注意,我需要注意所有句子(這意味着我可以擁有兩個以上的英語單詞)。

如何獲得如下結果?

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

我想也許可以隨時用英語單詞構建一個新字符串,將其反向並再次構建原始字符串,或者可以在引用字符串中使用...


感謝您的幫助,但我仍然遇到問題! 我已經按照您說的將句子拆分了,我顛倒了數組,我得到了:

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

在步驟2之后,希伯來語單詞的位置已磨損! 在步驟3中,我再次顛倒了希伯來語單詞,然后得到:

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

並且我需要切換它們的位置(一個接一個,因為我說我可以有一個包含很多單詞的句子..)所以,我不明白我是如何“將數組字符串放回一起的”(步驟5)

好吧,如果將問題分成幾部分,它將看起來像這樣。 你需要:

  1. 將句子拆分為字符串數組
  2. 反轉數組
  3. 檢測單詞是否在希伯來語中
  4. 如果不在希伯來語中,則需要反轉字符串
  5. 現在您只需要將字符串數組放回到一個字符串中就可以了!

編輯:我誤解了你的問題。 您可以執行以下操作:

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

Paolo為弄清楚算法做了艱苦的工作:

  1. 將句子拆分為字符串數組
  2. 反轉陣列
  3. 如果單詞不是希伯來語,則將其反轉
  4. 加入琴弦

這是使用LINQ的更優雅的版本:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM