replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box
replace duplicate words in text box replace duplicate words in text box
replace duplicate words in text box
Go Back  Xtreme Visual Basic Talk > > > replace duplicate words in text box


Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2006, 05:24 AM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default replace duplicate words in text box


i want to replace 2 duplicate words into 2 different words in just one line
i have multiple lines in this text i want to edit they all have 1 or 2 times the duplicate word


like:
time 12:00 and time 13:00
time 13:00 and time 24:00
time 15:00
to:
time1 12:00 and time2 13:00
time1 13:00 and time2 24:00
time1 15:00

is there a code for this that should be triggered by a buttom.

if search every where but could not find it

regards william

Last edited by w1ll14m; 02-11-2006 at 05:49 AM.
Reply With Quote
  #2  
Old 02-11-2006, 05:52 AM
Iceplug's Avatar
Iceplugreplace duplicate words in text box Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Yes, that's because you have to write it instead of looking for code.

A simple solution with Replace could be to replace
" time" with " time2", since the second time is always preceded by a space.
Secondly, another replace to get the remaining times would be to replace
"time " with "time1 "
of course, you'd have to do these in order otherwise the second replace would replace all "time"s with "time1"s.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 02-11-2006, 05:58 AM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

then i explained it wrong i think
the full line looks like:
"09:00 - 15:30 09:00 - 10:00 time 10:45 - 11:00 Lunch 12:15 - 12:45 time 14:00 - 14:15"
"12:00 - 21:00 time 13:30 - 13:45 Diner 17:00 - 17:30 time 19:15 - 19:30"
it should be:
"09:00 - 15:30 09:00 - 10:00 time1 10:45 - 11:00 Lunch 12:15 - 12:45 time2 14:00 - 14:15"
"12:00 - 21:00 time1 13:30 - 13:45 Diner 17:00 - 17:30 time2 19:15 - 19:30"

i have allready fixed the others only as you can see 'time' is duplicate en should be renamed per-line to time1 and time2

but thanx for thereply
Reply With Quote
  #4  
Old 02-11-2006, 06:10 AM
Iceplug's Avatar
Iceplugreplace duplicate words in text box Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Aww... that makes it harder.

However, if you are strictly using two times per line, you can use
.IndexOf and .LastIndexOf
to find the location of each time in the line.

First, use .IndexOf to find the location of "time" in the string - this will give you the location of the first time (the one that should be time1). The result you will put into a variable:
X = Strg.IndexOf("time")

You then use .Insert to insert a "1" at the position of X + 4.

AFter you do that, use .LastIndexOf exactly like you used .IndexOf to find the position of "time" - this will return the last one (the one where you want "time2")
You'll assign this result to a variable like you did for .IndexOf,
and you then use .Insert to insert a "2" at the position of X + 4
(or, if you didn't use X as the result of the .LastIndexOf. use whatever is appropriate).

Do you have more than one line in your textbox?

If so, then I think you'd have to loop through the .Lines() array or .Split the array, but I want to make sure if you have more than one line first.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 02-11-2006, 06:20 AM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

yeah about 20 to 50 lines per textbox.. and i am new to vb.net vb6 was easyer, just started today if you have a sample code it would be easy thanx allready
Reply With Quote
  #6  
Old 02-11-2006, 06:50 AM
Iceplug's Avatar
Iceplugreplace duplicate words in text box Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

We do not just give out sample code (FAQ) - you have to show some work on your end first.

Actually, what I'll recommend is a Split modify Join solution (good for modifying stuff on every line of something):
So, first what you do is declare a String array (Strgs):
Dim Strgs() As String

Split your initial text (Strg) into the string array (Strgs):
Strgs = Strg.Split(Environment.Newline)

Next, you'll loop through all of the items in the array using a For Loop
The loop will start at 0 and go to Strgs.GetUpperBound(0). That is because the first index of any array in .NET is 0 and .GetUpperBound method on your array will tell you the last index.
So, using your loop variable (LV), make the changes that I mentioned above.
X = Strgs(LV).IndexOf("time") etc.

After that you should end the loop because you are finished - the loop will take care of all of the lines (they will have "time1" and "time2" instead of "time")

Finally, join the string array into one big final string with String.Join(Strgs, Environment.Newline) to get the string array back to a single string.

Then you reassign this to the textbox and you should be done. Let me know if you get stuck somewhere.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #7  
Old 02-11-2006, 06:57 AM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

yeah i knew that this would be the answer but still thanx for helping me out
if it is correct .GetUpperBound is the last 'time' to replace ?

so this would be te code ?
Code:
        Dim strg() As String
        strg = strg.Split(Environment.NewLine)
        strg.GetUpperBound(0)
        strg.GetUpperBound(1)
        strg = strg(LV).IndexOf("time")
        String.Join(strg, Environment.NewLine)
        Me.unconverted.Text = strg
edit:

hmmm i feel i got stuck now:
Error 1 'Split' is not a member of 'System.Array'. D:\~~~~projects\noname-tool_AUX8 addon\gerda\Form2.vb 5 16 gerda
Error 2 Name 'LV' is not declared. D:\~~~~projects\noname-tool_AUX8 addon\gerda\Form2.vb 8 21 gerda
Error 3 Value of type '1-dimensional array of String' cannot be converted to 'String'. D:\~~~~projects\noname-tool_AUX8 addon\gerda\Form2.vb 9 21 gerda
Error 4 Value of type 'String' cannot be converted to '1-dimensional array of String'. D:\~~~~projects\noname-tool_AUX8 addon\gerda\Form2.vb 9 27 gerda
Error 5 Value of type '1-dimensional array of String' cannot be converted to 'String'. D:\~~~~projects\noname-tool_AUX8 addon\gerda\Form2.vb 10 31 gerda

i feel like a noob....

Last edited by w1ll14m; 02-11-2006 at 07:12 AM.
Reply With Quote
  #8  
Old 02-11-2006, 07:13 AM
Iceplug's Avatar
Iceplugreplace duplicate words in text box Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

You need the For Loop to go from 0 To strgs.GetUpperBound(0)
Also, you need to name the string array something different than the string that contains the text.

I said to use strgs above - also, don't forget to assign the text to strg.
As for the loop you will For LV = 0 To strgs.GetUpperBound(0)

And in the loop, you will use .IndexOf, .Insert, .LastIndexOf, and .Insert as I mentioned in post #4
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #9  
Old 02-11-2006, 07:50 AM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

Quote:
Originally Posted by Iceplug
You need the For Loop to go from 0 To strgs.GetUpperBound(0)
Also, you need to name the string array something different than the string that contains the text.

I said to use strgs above - also, don't forget to assign the text to strg.
As for the loop you will For LV = 0 To strgs.GetUpperBound(0)

And in the loop, you will use .IndexOf, .Insert, .LastIndexOf, and .Insert as I mentioned in post #4
This is what i have now,
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strg As String
        strg = Me.txtBox.Text
        Dim strgs() As String

        strgs = strg.Split(Environment.NewLine)
        strgs.GetUpperBound(0)
        strgs.GetUpperBound(0)
        strg = strg.IndexOf("time")
        strg = strg.Replace("time", "time1")
        strg = strg.LastIndexOf("time")
        strg = strg.Replace("time", "time2")
        String.Join(strgs(0), strgs)
        Me.txtBox.Text = strg
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.txtBox.Text = ""
    End Sub
End Class
but when i press the button 'try' i get as output '-1'

anything i forgot?
Reply With Quote
  #10  
Old 02-11-2006, 07:56 AM
Iceplug's Avatar
Iceplugreplace duplicate words in text box Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

First, that should be strg.Insert instead of strg.Replace. Your strg.IndexOf returns an Integer, which is the location of time in the string. Assign the result of strg.IndexOf() to an integer, such as
X = strg.IndexOf("time"). where you'd declare X as an integer
Dim X As Integer
Same with the .LastIndexOf and the .Replace statement that follows it.

Also, you still need to add the for loop. After you make the changes above (Replace should be Insert with appropriate arguments, and the result of IndexOf and LastIndexOf should be assigned to an Integer), you will want loop through al of the strings, you need to put the For LV = part (in previous post, we discussed what to loop through) that will go before the IndexOf, Insert, LastIndexOf and Insert sequence.

The Next goes after these, so that the statements inside will loop through and look at each line and make appropriate changes to each line.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #11  
Old 02-11-2006, 08:08 AM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

about the LV is that a string or an integer ?
Reply With Quote
  #12  
Old 02-11-2006, 12:34 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Strings.Replace (runtime function) can also be used, so you can simply search for "time " and replace it with what you want... Yes, I know that using runtime functions are not always the best bet, and I may get bashed for suggesting this, but in this case, you can use it to specify the number of substitutions. You simply replace the first "time " (with the space) with "time1 " and tell it to substitute one time, then call it again for the second "time ", like below:

Code:
        'note the spaces after "time ", if not used you will end up with "time21" on the first time
        Dim MyString As String = "time 12:00 and time 13:00"
        MyString = Strings.Replace(MyString, "time ", "time1 ", , 1) 'the "1" means only replace one time
        MyString = Strings.Replace(MyString, "time ", "time2 ", , 1)
        MessageBox.Show(MyString)
This way you dont have to use .IndexOf, or split the strings into an array and make sure that you are changing the correct "time" string...
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...
Reply With Quote
  #13  
Old 02-11-2006, 01:07 PM
Iceplug's Avatar
Iceplugreplace duplicate words in text box Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

LV is an integer.
For LV = 0 To Strgs.GetUpperBound(0)
0 and Strgs.GetUpperBound(0) are both integer values.

If I recall correctly, Strings.Replace() would only replace the first occurence. While that would work for small strings such as one line, you'd still have to loop through all of the lines for it to work correctly.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #14  
Old 02-11-2006, 01:21 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Quote:
Originally Posted by Iceplug
If I recall correctly, Strings.Replace() would only replace the first occurence. While that would work for small strings such as one line, you'd still have to loop through all of the lines for it to work correctly.
Right, I thought he was reading it line by line, made that assumption by his first post, when he just stated that he needed to replace two instances of "time " in a string.... it only makes things more difficult if you try to read it all into one string and try to go from there, especially since he can possibly only have one "time " in a line, only makes sense to read it in Line by Line.....If having one "time" in the line, it will find nothing to replace on the second .Replace call in my suggestion, thus not changing anything, so those two lines of Strings.Replace would work for every line in the file and produce no problems.
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...

Last edited by gigemboy; 02-11-2006 at 01:58 PM.
Reply With Quote
  #15  
Old 02-11-2006, 02:14 PM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

Quote:
Originally Posted by Iceplug
LV is an integer.
For LV = 0 To Strgs.GetUpperBound(0)
0 and Strgs.GetUpperBound(0) are both integer values.

If I recall correctly, Strings.Replace() would only replace the first occurence. While that would work for small strings such as one line, you'd still have to loop through all of the lines for it to work correctly.
As you can see i still stuck in the .Insert

This is what i created for so far
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strg As String
        Dim output As String
        Dim strgs() As String
        For LV = 0 To strgs.GetUpperBound(0)

        Next
        strg = Me.txtBox.Text
        strgs = strg.Split(Environment.NewLine)
        strgs.GetUpperBound(0)
        strg = strg(LV).LastIndexOf("time")
        strg = strg.Insert()
        strg = strg.LastIndexOf("time")
        strg = strg.Insert()
        String.Join(strg, Environment.NewLine)
        Me.txtBox.Text = strg
        strgs.GetUpperBound(5)

    End Sub
Only the string.join and the .Insert are new problems to me...
If i did it still wrong tell me
Reply With Quote
  #16  
Old 02-11-2006, 02:28 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

I see that you have yet to understand my simple solution to this, which does not require .Indexof, splits, arrays, or joins
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...
Reply With Quote
  #17  
Old 02-11-2006, 02:38 PM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

Quote:
Originally Posted by gigemboy
I see that you have yet to understand my simple solution to this, which does not require .Indexof, splits, arrays, or joins
yeah i know... i would use it if i had always the standard lines but the problem is that the lines are always different and they change every day, we are talking about 50-150 lines that the program should change, that's not possible if you do this manual.... so that is the reason i choose this script......
Reply With Quote
  #18  
Old 02-11-2006, 03:06 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Quote:
Originally Posted by w1ll14m
yeah i know... i would use it if i had always the standard lines but the problem is that the lines are always different and they change every day, we are talking about 50-150 lines that the program should change, that's not possible if you do this manual.... so that is the reason i choose this script......
Of course... I wouldnt want to do it manually at all either, that is why you LOOP it for every line in the file. Since you don't seem to grasp what I am telling you, here is how simple the entire process really is. Below, opens up test.txt, reads line by line, and writes the new file called output.txt, then opens output.txt at the end so you can see the results....
Code:
        'of course, replace "c:\test.txt" with your file
        Dim MyReader As New System.IO.StreamReader("c:\test.txt")
        Dim MyWriter As New System.IO.StreamWriter("c:\output.txt")
        Dim MyString As String
        While MyReader.Peek <> -1
            MyString = MyReader.ReadLine
            MyString = Strings.Replace(MyString, "time ", "time1 ", , 1)
            MyString = Strings.Replace(MyString, "time ", "time2 ", , 1)
            MyWriter.WriteLine(MyString)
        End While
        MyWriter.Close()
        MyReader.Close()
        System.Diagnostics.Process.Start("c:\output.txt")
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...
Reply With Quote
  #19  
Old 02-11-2006, 03:56 PM
w1ll14m w1ll14m is offline
Freshman
 
Join Date: Jun 2005
Posts: 36
Default

Quote:
Originally Posted by gigemboy
Of course... I wouldnt want to do it manually at all either, that is why you LOOP it for every line in the file. Since you don't seem to grasp what I am telling you, here is how simple the entire process really is. Below, opens up test.txt, reads line by line, and writes the new file called output.txt, then opens output.txt at the end so you can see the results....
Code:
        'of course, replace "c:\test.txt" with your file
        Dim MyReader As New System.IO.StreamReader("c:\test.txt")
        Dim MyWriter As New System.IO.StreamWriter("c:\output.txt")
        Dim MyString As String
        While MyReader.Peek <> -1
            MyString = MyReader.ReadLine
            MyString = Strings.Replace(MyString, "time ", "time1 ", , 1)
            MyString = Strings.Replace(MyString, "time ", "time2 ", , 1)
            MyWriter.WriteLine(MyString)
        End While
        MyWriter.Close()
        MyReader.Close()
        System.Diagnostics.Process.Start("c:\output.txt")
also this one works almost complete but again when 'time-2' does not exist the next time in the txt document shall be named time2 so this will mess up the full output of my program.... so this is the problem and this is why i choose to use the difficult code to correct this error.

regards william
Reply With Quote
  #20  
Old 02-11-2006, 04:22 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Quote:
Originally Posted by w1ll14m
also this one works almost complete but again when 'time-2' does not exist the next time in the txt document shall be named time2 so this will mess up the full output of my program....
???? It does not error at all, unless your text file is different than what you had posted... you had posted three lines of text...

Quote:
time 12:00 and time 13:00
time 13:00 and time 24:00
time 15:00
That text is seperated on three different lines... , and my code posted does fine with the "time 15:00" on the one line, it replaces it with "time1 15:00", then reads in the next line and changes it fine. The text I tested it with had this:
Code:
time 12:00 and time 13:00      'outputs "time1 12:00 and time2 13:00"
time 13:00 and time 24:00      'outputs "time1 13:00 and time2 24:00"
time 15:00                     'outputs "time1 15:00"
time 12:00 and time 13:00      'outputs "time1 12:00 and time2 13:00"
time 13:00                     'outputs "time1 13:00"
time 15:00                     'outputs "time1 15:00"
time 12:00 and time 13:00      'outputs "time1 12:00 and time2 13:00"
time 13:00 and time 24:00      'outputs "time1 13:00 and time2 24:00"
time 15:00                     'outputs "time1 15:00"
Each was in its own line, with a linebreak, and all read in and changed fine. The only way it would not work, is if this text is not actually seperated like you showed, and was something like this:
Code:
time 15:00 time 12:00 and time 13:00 time 13:00 
time 24:00 time 15:00
time 12:00
Or something to that matter to where more than two "time " strings existed in one line of text. If that was the case, then you should have specified that, as it is a very important piece of information to have...

Why dont you copy and paste in some text from your text file... the linebreaks should show up as new lines when you paste... then we can see what your text file actually contains...
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...

Last edited by gigemboy; 02-11-2006 at 04:35 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box replace duplicate words in text box
replace duplicate words in text box
replace duplicate words in text box
 
replace duplicate words in text box
replace duplicate words in text box
 
-->