Split html into words

Suppose I have the following line:

Hellotoevryone<img height="115" width="150" alt="" src="/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsogladtoseeall.

      

This line represents a sequence of characters that are not separated by a space, this line also has an html image added. Now I want to split the string into words each 10 characters long, so the aoutput should be:

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.

      

So the idea is to store the content of the html tag as 0 length char.

I wrote a method like this, but it ignores html tags:

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

      

I would like to see some code that does the splitting as I described. Thanks.

+1


a source to share


2 answers


This next code will handle the case you provided, but breaks down for something more complex. Also, since you didn't specify how it should handle long-form tags with inner text or HTML, it treats all tags as short forms (run the code to see what I mean).

Works with this input:

Hellotoevryone <img height = "115" width = "150" alt = "" src = "/ Content / Edt / image / b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" /> Iamsogladtoseeall.
Hellotoevryone <img src = "/ Content / Edt / image / b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" /> Iamsoglad <img src = "baz.jpeg" /> toseeall.
Hello <span class = "foo"> toevryone </span> Iamso <em> glad </em> toseeallTheQuickBrown <img src = "bar.jpeg" /> FoxJumpsOverTheLazyDog.
Hello <span class = "foo"> toevryone </span> Iamso <em> glad </em> toseeall.
Loremipsumdolorsitamet, consecteturadipiscingelit, Nullamacnibhelit, quisvolutpatnunc.Donecultrices, ipsumquisaccumsanconvallis, tortortortorgravidaante, etsollicitudinipsumnequeeulorem.


Break with this input (note the incomplete tag):

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

      

+1


a source







All Articles