c#用两种语言拆分和反转句子 [英] c# split and reverse sentence with two languages

查看:57
本文介绍了c#用两种语言拆分和反转句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

  1. array[0] = אוראל
  2. array[1] = was
  3. array[2] = 这里
  4. array[3] = :ואמר

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

如何得到如下所示的结果?

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

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

<小时>

感谢您的帮助,但我还是遇到了问题!我像你说的那样拆分了句子,我颠倒了数组,然后我明白了:

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

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

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

我需要改变他们的位置(一对一,正如我所说的,我可以有很多词的句子..)所以,我不明白我是如何将数组字符串重新组合在一起"(第 5 步)

解决方案

Paolo 为找出算法付出了艰辛的努力:

<块引用>

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

这是一个使用 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:

  1. array[0] = אוראל
  2. array[1] = was
  3. array[2] = here
  4. 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:

  1. Split a sentence into an array of strings
  2. Reverse the Array
  3. If a word is not in Hebrew reverse it
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆