问题
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)
回答1:
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(" ")
回答2:
Well, if you split the problem into pieces, it would look like this. You need to:
- Split a sentence into an array of strings
- reverse the Array
- Detect if a word is in Hebrew
- if it is not in Hebrew you need to Reverse the string
- now you just need to put the array of strings back together in one string and you're done!
EDIT: I misunderstood your problem. You could do something like this:
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);
}
来源:https://stackoverflow.com/questions/9622610/c-sharp-split-and-reverse-sentence-with-two-languages