我有这样的句子(希伯来语,RTL):
ואמר在这里אוראל
现在,当我将其插入数组时,我得到了:
现在,因为希伯来语是一种RTL语言,我需要翻转英文字母,我需要切换位置。 请注意,我需要处理任何句子(这意味着我可以有两个以上的英语单词)。
如何让结果如下所示?
ואמרereh看到了אוראל
我想也许可以用英语单词构建一个新字符串,反转它并再次构建原始字符串,或者可以在ref字符串中使用...
感谢您的帮助,但我仍然遇到了问题! 正如你所说,我把句子分开,我颠倒了阵列 我明白了:
לארואereh看到了רמאו
在第2步之后,希伯来语的位置已经过了! 在第3步中,我再次颠倒了希伯来语,我得到了:
אוראלereh看到ואמר
我需要改变他们的位置(一对一,正如我所说的那样 有一个有很多单词的句子..) 所以,我不明白我是如何“将数组串重新组合在一起”(步骤5)
答案 0 :(得分: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);
}
答案 1 :(得分:0)
- 将句子拆分为字符串数组
- 反转数组
- 如果一个单词不是希伯来语,那么
- 加入字符串
醇>
这是一个使用LINQ的更优雅的版本:
var result = Sentence
.Split(' ')
.Reverse()
.Select(w => IsHebrew(w) ? w : new String(w.Reverse().ToArray())
.Join(" ")