Counting String tokens precisly in an html document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    Counting String tokens precisly in an html document

    Hi there,

    I have an html file like this.
    ----------------------------------------
    <body>
    <h1>Home Page</h1>
    <p>
    Welcome<br>To<b r>

    <br> My Home Page
    </p>
    --------------------------------------------

    I want to know exact number of string tokens

    it should discard the new lines (But completely discarding them will result
    in merging the words seperated by new lines), discard too much white space
    etc.

    Please, some help will be appreciated.

    I wrote this function initially, which only works with single white space.

    public int count_body(stri ng s)
    {
    char[] sp = {' '};
    int count = s.Split(sp).Len gth;
    return count;
    }

    Thanks!!


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Counting String tokens precisly in an html document

    kman,

    You are better off using an HTML parser for something like this. You
    can use MSHTML through interop (Microsoft's HTML parser), and then access
    the innerText property to get just the text for the document. You can then
    parse apart that text easily (it should be broken properly, even with the BR
    tags in between, which you won't see in the innerText).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <kman@yahoo.com > wrote in message news:nbSdnWJytd uMN8PfRVn-rw@rogers.com.. .[color=blue]
    > Hi there,
    >
    > I have an html file like this.
    > ----------------------------------------
    > <body>
    > <h1>Home Page</h1>
    > <p>
    > Welcome<br>To<b r>
    >
    > <br> My Home Page
    > </p>
    > --------------------------------------------
    >
    > I want to know exact number of string tokens
    >
    > it should discard the new lines (But completely discarding them will
    > result in merging the words seperated by new lines), discard too much
    > white space etc.
    >
    > Please, some help will be appreciated.
    >
    > I wrote this function initially, which only works with single white space.
    >
    > public int count_body(stri ng s)
    > {
    > char[] sp = {' '};
    > int count = s.Split(sp).Len gth;
    > return count;
    > }
    >
    > Thanks!!
    >
    >[/color]


    Comment

    • Vladimir Granitsky

      #3
      Re: Counting String tokens precisly in an html document

      Try using regular expressions

      <kman@yahoo.com > wrote in message news:nbSdnWJytd uMN8PfRVn-rw@rogers.com.. .[color=blue]
      > Hi there,
      >
      > I have an html file like this.
      > ----------------------------------------
      > <body>
      > <h1>Home Page</h1>
      > <p>
      > Welcome<br>To<b r>
      >
      > <br> My Home Page
      > </p>
      > --------------------------------------------
      >
      > I want to know exact number of string tokens
      >
      > it should discard the new lines (But completely discarding them will[/color]
      result[color=blue]
      > in merging the words seperated by new lines), discard too much white space
      > etc.
      >
      > Please, some help will be appreciated.
      >
      > I wrote this function initially, which only works with single white space.
      >
      > public int count_body(stri ng s)
      > {
      > char[] sp = {' '};
      > int count = s.Split(sp).Len gth;
      > return count;
      > }
      >
      > Thanks!!
      >
      >[/color]


      Comment

      Working...