1

次の文字列があると仮定します。

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);
}

説明したように分割を実行するコードを見たいのですが。ありがとうございます。

4

2 に答える 2

1

この次のコードは、提供されたケースを処理しますが、より複雑な場合は中断します。また、内部テキストまたは HTML を含む長い形式のタグを処理する方法を指定していないため、すべてのタグを短い形式のタグとして扱います (コードを実行して、意味を確認してください)。

この入力で動作します:

こんにちは。
こんにちは。
こんにちは<span class="foo">toevryone</span>Iamso<em>嬉しい</em>すべての TheQuickBrown<img src="bar.jpeg" />FoxJumpsOverTheLazyDog をご覧ください。
こんにちは<span class="foo">toevryone</span>Iamso<em>うれしい</em>みんなに会えます。
Loremipsumdolorsitamet,consecteturadipiscingelit.Nullamacnibhelit,quisvolutpatnunc.Donecultrices,ipsumquisaccumsanconvallis,tortortortorgravidaante,etsollicitudinipsumnequeeulorem.

この入力で中断します (不完全なタグに注意してください):

Hellotoevryone<img height="115" width="150" alt="" src="/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" /Iamsogladtoseeall.
using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;

public static class CustomSplit {
  public static void Main(String[] args) {
    if (args.Length > 0 && File.Exists(args[0])) {
      StreamReader sr = new StreamReader(args[0]);
      String[] lines = sr.ReadToEnd().Split(new String[]{Environment.NewLine}, StringSplitOptions.None);

      int counter = 0;
      foreach (String line in lines) {
        Console.WriteLine("########### Line {0} ###########", ++counter);
        Console.WriteLine(line);
        Console.WriteLine(line.EnsureWordLength(10));
      }
    }
  }

}

public static class EnsureWordLengthExtension {
  public static String EnsureWordLength(this String target, int length) {
    List<List<Char>> words = new List<List<Char>>();

    words.Add(new List<Char>());

    for (int i = 0; i < target.Length; i++) {
      words[words.Count - 1].Add(target[i]);

      if (target[i] == '<') {
        do {
          i++;
          words[words.Count - 1].Add(target[i]);
        } while(target[i] != '>');
      }

      if ((new String(words[words.Count - 1].ToArray())).CountCharsWithoutTags() == length) {
        words.Add(new List<Char>());
      }
    }

    String[] result = new String[words.Count];
    for (int j = 0; j < words.Count; j++) {
      result[j] = new String(words[j].ToArray());
    }

    return String.Join(" ", result);
  }

  private static int CountCharsWithoutTags(this String target) {
    return Regex.Replace(target, "<.*?>", "").Length;
  }
}
于 2009-05-10T17:20:21.910 に答える