次の文字列があると仮定します。
Hellotoevryone<img height="115" width="150" alt="" src="/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsogladtoseeall.
この文字列は、スペースで区切られていない一連の文字を表します。この文字列には、html画像も挿入されています。ここで、文字列を単語に分割します。各単語の長さは10文字なので、出力は次のようになります。
1)Hellotoevr
2)yone<img height="115" width="150" alt="" src="/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsog
3)ladtoseeal
4)l.
したがって、HTMLタグのコンテンツを長さ0文字として保持するという考え方です。
私はそのようなメソッドを作成しましたが、htmlタグは考慮されていません。
public static string EnsureWordLength(this string target, int length)
{
string[] words = target.Split(' ');
for (int i = 0; i < words.Length; i++)
if (words[i].Length > length)
{
var possible = true;
var ord = 1;
do
{
var lengthTmp = length*ord+ord-1;
if (lengthTmp < words[i].Length) words[i] = words[i].Insert(lengthTmp, " ");
else possible = false;
ord++;
} while (possible);
}
return string.Join(" ", words);
}
説明したように分割を実行するコードを見たいのですが。ありがとうございます。