c#用两种语言拆分和反转句子 [英] c# split and reverse sentence with two languages
问题描述
我有一个这样的句子(希伯来语,RTL):
ואמר在这里אוראל
现在,当我将其插入数组时,我得到了:
- array[0] = אוראל
- array[1] = was
- array[2] = 这里
- array[3] = :ואמר
现在,因为希伯来语是一种 RTL 语言,我需要颠倒英文字母,我需要切换位置.请注意,我需要处理任何句子(这意味着我可以有两个以上的英语单词).
如何得到如下所示的结果?
ואמר ereh看到了אוראל
我想也许可以随时使用英文单词构建一个新字符串,将其反转并再次构建原始字符串,或者可能在引用字符串中使用...
<小时>感谢您的帮助,但我还是遇到了问题!我像你说的那样拆分了句子,我颠倒了数组,然后我明白了:
לארוא ereh看到了רמאו
在第 2 步之后,希伯来语单词的位置被磨损了!在第 3 步中,我再次颠倒了希伯来语单词,我得到了:
אוראל ereh看到了ואמר
我需要改变他们的位置(一对一,正如我所说的,我可以有很多词的句子..)所以,我不明白我是如何将数组字符串重新组合在一起"(第 5 步)
Paolo 为找出算法付出了艰辛的努力:
<块引用>- 将一个句子拆分成一个字符串数组
- 反转数组
- 如果单词不是希伯来语,则将其反转
- 加入字符串
这是一个使用 LINQ 的更优雅的版本:
var 结果 = 句子.分裂(' ').撤销().Select(w => IsHebrew(w) ? w : new String(w.Reverse().ToArray()).加入(" ")
I have a sentence like this (in Hebrew,RTL):
ואמר was here אוראל
Now, when I insert this into array I got:
- array[0] = אוראל
- array[1] = was
- array[2] = here
- 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)
Paolo did the hard work of figuring out the algorithm:
- Split a sentence into an array of strings
- Reverse the Array
- If a word is not in Hebrew reverse it
- 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(" ")
这篇关于c#用两种语言拆分和反转句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!