1

重複の可能性:
単語全体を保持して文字列を分割する方法は?

次の文字列があります。

string s = "The Electors shall meet in their respective states to vote by ballot for President and Vice-President.";

最初の 60 文字を取得して、それぞれ 30 文字以下の 2 つの別個の文字列に分割したいと考えています。各文字列は単語全体で始まる必要があります (単語の一部やスペースは使用できません)。したがって、これは望ましい結果です。

string s1 = "The Electors shall meet in"; // 26 characters
string s2 = "their respective states to vot"; // 30 characters

ありがとうございました。

4

2 に答える 2

0

中点を計算してから、スペースが見つかるまで両方向に作業してみてください。それがあなたの分割点になります。

于 2012-09-26T21:26:47.347 に答える
0
string s = "The Electors shall meet in their respective states to vote by ballot for President and Vice-President.";

string sSub = s.Substring(0,60);  //first 60 letters

string sSubSub = sSub.Substring(0,30);  //at most 30 per string

int index = sSubSub.LastIndexOf(' '); //finds the last space

string firstString = sSub.Substring(0,index); //first string is up until that space of t he 60-letter string

string secondSTring = sSub.Substring(index + 1, 30); //second string is the first 30 letters of the rest of the 60-letter string
于 2012-09-26T21:31:26.450 に答える