Results 1 to 5 of 5

Thread: [RESOLVED] Numbering repeated words

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2010
    Posts
    130

    Resolved [RESOLVED] Numbering repeated words

    How to delete repeated and numbering words from the text?
    For example:

    * in red: Words / numbers repeated
    * my textbox:

    " test1 test1 bb what how bb so and then yes so then so 1 3 1 4"

    to get this result:
    *in green: The number of times the word / number show in the text
    so (3)
    then (2)
    1 (2)
    Test1 (2)
    bb (2)
    what (1)
    how (1)

  2. #2
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Numbering repeated words

    You can create a Hashtable and use the word as a key. Everytime you find a word, add 1 to the value stored there.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Numbering repeated words

    try this:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim s As String = " test1 test1 bb what how bb so and then yes so then so 1 3 1 4"
    3.     Dim occurences = From word In s.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries) _
    4.                  Group word By word Into Count()
    5.     Dim msg As String = ""
    6.     For Each o In occurences
    7.         msg &= String.Format("{0,-10} {1}{2}", o.word & ",", o.Count, Environment.NewLine)
    8.     Next
    9.     MsgBox(msg)
    10. End Sub

  4. #4
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Numbering repeated words

    I've just been playing with Regex so thought i'd add another method

    VB Code:
    1. Imports System.Text.RegularExpressions
    2. Imports System.Text
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim Input As String = "test1 test1 bb what how bb so and then yes so then so 1 3 1 4"
    8.  
    9.         Dim DupeCheck As New ArrayList
    10.         Dim Results As New StringBuilder
    11.         For Each Word As Match In Regex.Matches(Input, "\b\w+\b").Cast(Of Match).ToArray
    12.             If Not (DupeCheck.Contains(Word.Value)) Then
    13.                 DupeCheck.Add(Word.Value)
    14.                 Results.Append(Word.Value & ": " & Regex.Matches(Input, String.Format("\b{0}\b", Word.Value)).Cast(Of Match).ToArray.Count).AppendLine()
    15.             End If
    16.         Next
    17.  
    18.         MsgBox(Input & vbCrLf & vbCrLf & Results.ToString)
    19.     End Sub
    20.  
    21. End Class

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2010
    Posts
    130

    Re: Numbering repeated words

    MarMan: Yes, but I do not manually insert the text so I do not understand how to set up random word key. thx for reply.

    .paul.: thx u agin and agin . ur code is good,but if u do new line it Jamming the text. (Is it my fault - I did not say that)

    jay20aiii thx agin, ur code its perfect for what I want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width