2016-05-17 15 views
1

私は、ユーザーがリッチテキストボックスに文章を入力できるようにして、データをリストに保存し、重複を削除しました。リストのデータをその位置に置き換える

私が知りたいのは、リストの単語をその位置で上書きし、元の文の位置をそれらの位置に置き換える方法です。

例:Hello this is a test I hope this test worksの文章が保存され、重複が削除され、hello, this, is, a, test, I, hope, worksという出力がある場合、コードでは1 2 3 4 5 6 7 8(これは)と置き換えられます。

ここでは、リスト内の実際の単語を元の位置に置き換えて、最後に1 2 3 4 5 6 7 2 5 8がカンマで区切られていると言います。

これは私のコードです:

string sentence = richTextBox1.Text; 

list = sentence.Split(delimiterChars).ToList(); 
listoriginal = sentence.Split(delimiterChars).ToList(); 
listBox1.Items.Add("Full sentence: " + String.Join(" ", list)); 
list = list.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList(); 
listBox1.Items.Add("Words in the input: " + String.Join(", ", list)); 

for (int i = 0; i < list.Count; i++) 
{ 
    list[i] = list[i].ToString(); 
    listoriginal[i] = listoriginal[i].ToString(); 
    resultList = listoriginal.Select(x => x.Replace(listoriginal[i], list[i])).ToList(); 
    i++; 
} 

listBox1.Items.Add("Final result: " + String.Join(", ", resultList)); 

答えて

0

最も簡単な方法は、辞書のようなキー/値ストアを使用するために、おそらくです - あなたは、重複を排除した後、あなたは辞書や使用にキー/値をプッシュすることができます文章を再構築すること。

// Create dictionary 
var dict = new Dictionary<string, int>(); 

// When looping through the words to determine index add each word to the dict - the word being the key and the value being the index 
dict.Add(word, index); 

その後

foreach(var word in words) { 
    // Get the value from the dictionary for the associated key (the key being the word) 
    var index = dict[word]; 
    // do stuff with index 
} 

(あなたはすべての単語をインデックス化しているので)あなたは、文字列を再構築するのではなく交換する文字列の連結を使用することができます。

一般的なアドバイスは、文字列は不変であるため、パフォーマンス/メモリを懸念しているときStringBuilderを使用するようになります

var sb = new StringBuilder(); 

foreach(var word in words) { 
    sb.Append(dict[word]); 
    sb.Append(" "); 
} 

sb.ToString(); 

編集:ここでは

より完全な例だ...

var sentence = "hello world this is a test hello world"; 

var words = sentence.Split(' '); 
var distinctWords = words.Distinct(StringComparer.InvariantCultureIgnoreCase); 
var dict = new Dictionary<string, int>(); 
var ix = 0; 
foreach (var word in distinctWords) 
{ 
    dict.Add(word.ToLower(), ix++); 
} 

var sb = new StringBuilder(); 
foreach (var word in words) 
{ 
    sb.Append(dict[word.ToLower()]); 
    sb.Append(" "); 
} 

// sb.ToString(); 
// 0 1 2 3 4 5 0 1 

明らかに私は置換をしていないので、これは元の文字列のフォーマットに影響するかもしれませんが、それはあなたにアイデアを与えます。 replaceを使うことはできますが、それはずっと遅くなりますが、文字列の長さと処理の量によって異なります。

+0

インデックス用として、私はあなたのソリューションを使用しようとしていますが、どのように私は言葉やインデックスになるだろう、単語がリスト[i]があるか、私は間違っているだろう、私は個別の拡張メソッドを使用して単語の別のリストを取得し、それらをループし、インクリメントカウンタ(0から始まる)を使用して辞書にそれぞれをプッシュするときに、それが –

+0

であるかどうか分かりません。それはあなたが文章などを構築するために使用できるキー付きリストです – Charleh

0

既に対処済みの単語で辞書を作成します。

Dictionary<int, string> words = new Dictionary<int, string>(); 
words.Items.Add(//number, //word); 

(こんにちは、1) (本、2) (である、3) (4) .....などに

:あなたは以下のだろうことを意味

あなたがその単語がすでに保存されているかどうかを確認するために検索するAAメソッドを書くことができ、例えば:

foreach(KeyValuePair<int, string> set in words) 
{ 
    if(set.value == //whatever word is next) 
    { 
     //write the number in the dictionary here 
     //the corresponding number can be grabbed using: set.key 
    } 
} 
0

は、基本的には、フライ単語の結果位置を保持Dictionary<string, int>に移入する必要があります。結果語は、その後Keysプロパティから取得することができます。

var originalWords = sentence.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries); 
var uniqueWordPositions = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase); 
var originalWordPositions = new List<int>(); 
foreach (var word in originalWords) 
{ 
    int position; 
    if (!uniqueWordPositions.TryGetValue(word, out position)) 
     uniqueWordPositions.Add(word, position = uniqueWordPositions.Count + 1); 
    originalWordPositions.Add(position); 
}; 
listBox1.Items.Add("Full sentence: " + string.Join(" ", originalWords)); 
listBox1.Items.Add("Words in the input: " + string.Join(", ", uniqueWordPositions.Keys)); 
listBox1.Items.Add("Final result: " + string.Join(", ", originalWordPositions));