|
-
Dec 20th, 2005, 08:45 AM
#1
Thread Starter
Fanatic Member
Text Manipulation
I have a list of text. Each line in the list is a string, then ZZZ then a string. What I want is to cut out from the beginning of the line up to and including ZZZ. Then I want to replace the letter K in each string with J. How would I do this?
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 08:51 AM
#2
Re: Text Manipulation
Give me an example of the string.
-
Dec 20th, 2005, 08:52 AM
#3
Thread Starter
Fanatic Member
Re: Text Manipulation
blahkblahXXX bbbbbbbbbbb
This obviously isnt the real string but its like it will be. So I wanna cut from the beinning to XXX including it. Then make the k a j.
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 09:22 AM
#4
Thread Starter
Fanatic Member
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 09:40 AM
#5
Re: Text Manipulation
Try something like this
VB Code:
Private Sub Command1_Click()
Dim strString As String
strString = "blahkblahXXX bbbbbbbbbbb"
strString = Replace(strString, "k", "j")
strString = Replace(strString, "X", "")
MsgBox strString
End Sub
-
Dec 20th, 2005, 09:42 AM
#6
Re: Text Manipulation
Try this:
VB Code:
Dim sText As String
sText = Replace(Split("blahkblahXXX bbbbbbbbbbb", "XXX")(0) & "XXX", "k", "j")
Debug.Print (sText)
-
Dec 20th, 2005, 09:42 AM
#7
Thread Starter
Fanatic Member
Re: Text Manipulation
I need to cut of the rest of the string. Starting with the space to the end.
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 09:45 AM
#8
Re: Text Manipulation
 Originally Posted by Steve_F
I need to cut of the rest of the string. Starting with the space to the end.
One modification
VB Code:
Private Sub Command1_Click()
Dim strString As String
Dim arrString() As String
strString = "blahkblahXXX bbbbbbbbbbb"
strString = Replace(strString, "k", "j")
strString = Replace(strString, "X", "")
arrString = Split(strString, " ")
MsgBox arrString(0)
End Sub
-
Dec 20th, 2005, 09:49 AM
#9
Thread Starter
Fanatic Member
Re: Text Manipulation
This is all getting confusing. Lemme just explain what I have. I wanna load a list which lines will resemble this :
email(AT)hotmail.com Fargo, Nd Blah
I wanna load the list. Take that excess at the end away. And change (AT) to @. Here is what I have :
VB Code:
Private Sub cmdStart_Click()
'load names
Dim strBuff As String
'Clear the ListBox contents
List1.Clear
intFF = FreeFile
Open App.Path & "\load.txt" For Input As #intFF
Do Until EOF(1)
Line Input #intFF, strBuff
List1.AddItem strBuff
Loop
Close #intFF
Label3.BackColor = &HFF00&
'remove excess
Label4.BackColor = &HFF00&
'replace text
Dim strString As String
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = Replace(strString, "(at)", "@")
List1.Selected(intIdx) = strString
Next
Label5.BackColor = &HFF00&
'save new file
Dim intCount As Integer
Dim intIdx As Integer
intCount = List2.ListCount - 1
Open App.Path & "\result.txt" For Output As #1
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #1, List1.List(intIdx)
Next
Close #1
Label6.BackColor = &HFF00&
End Sub
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 09:53 AM
#10
Re: Text Manipulation
You can use Split to get rid of the rest (your Replace looks fine)
VB Code:
Dim strString As String
Dim arrString As String
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = Replace(strString, "(at)", "@")
arrString = Split(strString, " ")
List1.Selected(intIdx) = arrString(0)
Next
That should work.
-
Dec 20th, 2005, 09:55 AM
#11
Re: Text Manipulation
Did you try the example I posted before?
VB Code:
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = Replace(Split(strString, " ")(0), "(at)", "@", , , vbTextCompare)
List1.Selected(intIdx) = strString
Next
-
Dec 20th, 2005, 09:55 AM
#12
Thread Starter
Fanatic Member
Re: Text Manipulation
Error "Expected array" on arrString(0)
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 09:57 AM
#13
Thread Starter
Fanatic Member
Re: Text Manipulation
So I used the second one. But its not working... It says its dont but doesnt write to file :
VB Code:
Private Sub cmdStart_Click()
'load names
Dim strBuff As String
'Clear the ListBox contents
List1.Clear
intFF = FreeFile
Open App.Path & "\load.txt" For Input As #intFF
Do Until EOF(1)
Line Input #intFF, strBuff
List1.AddItem strBuff
Loop
Close #intFF
Label3.BackColor = &HFF00&
'remove excess
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = Replace(Split(strString, " ")(0), "(at)", "@", , , vbTextCompare)
List1.Selected(intIdx) = strString
Next
Label4.BackColor = &HFF00&
'replace text
Label5.BackColor = &HFF00&
'save new file
intCount = List1.ListCount - 1
Open App.Path & "\emails.txt" For Output As #1
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #1, List1.List(intIdx)
Next
Close #1
Label6.BackColor = &HFF00&
End Sub
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 09:58 AM
#14
Re: Text Manipulation
 Originally Posted by Steve_F
Error "Expected array" on arrString(0)
Thats because I'm moron.
You need to change
VB Code:
Dim arrString As String
'change to
Dim arrString() As String
Sorry 'bout that.
-
Dec 20th, 2005, 10:00 AM
#15
Thread Starter
Fanatic Member
Re: Text Manipulation
No prob lol. So it's not saving right now..
VB Code:
Private Sub cmdStart_Click()
'load names
Dim strBuff As String
'Clear the ListBox contents
List1.Clear
intFF = FreeFile
Open App.Path & "\load.txt" For Input As #intFF
Do Until EOF(1)
Line Input #intFF, strBuff
List1.AddItem strBuff
Loop
Close #intFF
Label3.BackColor = &HFF00&
'remove excess
Dim strString As String
Dim arrString() As String
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = Replace(strString, "(at)", "@")
arrString = Split(strString, " ")
List1.Selected(intIdx) = arrString(0)
Next
Label4.BackColor = &HFF00&
'replace text
Label5.BackColor = &HFF00&
'save new file
intCount = List1.ListCount - 1
Open App.Path & "\emails.txt" For Output As #1
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #1, List1.List(intIdx)
Next
Close #1
Label6.BackColor = &HFF00&
End Sub
It doesnt save anything.
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:02 AM
#16
Re: Text Manipulation
As you loop through the listbox are items selected?
If not, then If List1.Selected(intIdx) will always equal False which means your Print code will never execute.
-
Dec 20th, 2005, 10:03 AM
#17
Re: Text Manipulation
 Originally Posted by Steve_F
So I used the second one. But its not working... It says its dont but doesnt write to file :
VB Code:
[SNIP]
'save new file
intCount = List1.ListCount - 1
Open App.Path & "\emails.txt" For Output As #1
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #1, List1.List(intIdx)
Next
Close #1
Label6.BackColor = &HFF00&
End Sub
Replace the #1 with your intFF variable, and assign it to Freefile again. It's possible that the filehandle isn't available because you closed it so recently.
-
Dec 20th, 2005, 10:04 AM
#18
Thread Starter
Fanatic Member
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:08 AM
#19
Re: Text Manipulation
VB Code:
intFF = FreeFile
intCount = List1.ListCount - 1
Open App.Path & "\emails.txt" For Output As #intFF
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx)
Next
Close #intFF
-
Dec 20th, 2005, 10:10 AM
#20
Thread Starter
Fanatic Member
Re: Text Manipulation
Doesnt work :
VB Code:
Private Sub cmdStart_Click()
'load names
Dim strBuff As String
'Clear the ListBox contents
List1.Clear
intFF = FreeFile
Open App.Path & "\load.txt" For Input As #intFF
Do Until EOF(1)
Line Input #intFF, strBuff
List1.AddItem strBuff
Loop
Close #intFF
Label3.BackColor = &HFF00&
'remove excess
Dim strString As String
Dim arrString() As String
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = Replace(strString, "(at)", "@")
arrString = Split(strString, " ")
List1.Selected(intIdx) = arrString(0)
Next
Label4.BackColor = &HFF00&
'replace text
Label5.BackColor = &HFF00&
'save new file
intFF = FreeFile
intCount = List1.ListCount - 1
Open App.Path & "\emails.txt" For Output As #intFF
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx)
Next
Close #intFF
Label6.BackColor = &HFF00&
End Sub
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:17 AM
#21
Re: Text Manipulation
Hack is right about the selection BTW, if this is all in one sub then there will never be any selected items because you clear and reload the List. I had thought that you might have combined code from a couple different subs in your post (the Dim statements throughout the code block threw me off).
-
Dec 20th, 2005, 10:21 AM
#22
Thread Starter
Fanatic Member
Re: Text Manipulation
So it now looks like this but still doesnt work :
VB Code:
Private Sub cmdStart_Click()
loadlist
replace
saveit
End Sub
Private Sub loadlist()
Dim strBuff As String
'Clear the ListBox contents
List1.Clear
intFF = FreeFile
Open App.Path & "\load.txt" For Input As #intFF
Do Until EOF(1)
Line Input #intFF, strBuff
List1.AddItem strBuff
Loop
Close #intFF
Label3.BackColor = &HFF00&
End Sub
Private Sub replace()
'remove excess
Dim strString As String
Dim arrString() As String
For intIdx = 0 To intCount
strString = List1.Selected(intIdx)
strString = replace(strString, "(at)", "@")
arrString = Split(strString, " ")
List1.Selected(intIdx) = arrString(0)
Next
Label4.BackColor = &HFF00&
'replace text
Label5.BackColor = &HFF00&
End Sub
Private Sub saveit()
'save new file
intFF = FreeFile
intCount = List1.ListCount - 1
Open App.Path & "\emails.txt" For Output As #intFF
For intIdx = 0 To intCount
If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx)
Next
Close #intFF
Label6.BackColor = &HFF00&
End Sub
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:30 AM
#23
Re: Text Manipulation
After your Replace sub has run, what are the contents of your listbox?
-
Dec 20th, 2005, 10:31 AM
#24
Thread Starter
Fanatic Member
Re: Text Manipulation
I dont know because there is an error in this line
VB Code:
strString = replace(strString, "(at)", "@")
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:32 AM
#25
Re: Text Manipulation
 Originally Posted by Steve_F
I dont know because there is an error in this line
VB Code:
strString = replace(strString, "(at)", "@")
What is the error?
-
Dec 20th, 2005, 10:35 AM
#26
Thread Starter
Fanatic Member
Re: Text Manipulation
Wrong number of arguments or invalid property assignment on the word replace
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:39 AM
#27
Re: Text Manipulation
Well, this was confusing until I released that you can't call a Sub Replace.
Replace is a VB reserved word, so rename your sub to something like ReplaceString.
-
Dec 20th, 2005, 10:40 AM
#28
Re: Text Manipulation
2 things. First, you need to either let the user select items from the list before you output it or just output the whole list. This means you need to load the list seperate from where you output it. Second, you should avoid using VB function names for your own functions. The code you just posted probably won't compile because your replace function doesn't accept arguments and you are calling it recursively. Try this:
VB Code:
Private Sub cmdStart_Click()
DoReplace
SaveIt
End Sub
Private Sub Form1_Load() 'Or where you want to load it.
Dim strBuff As String, intFF As Integer
List1.Clear
intFF = FreeFile
Open App.Path & "\load.txt" For Input As #intFF
Do Until EOF(intFF)
Line Input #intFF, strBuff
List1.AddItem strBuff
Loop
Close #intFF
End Sub
Private Sub DoReplace()
Dim intIdx As Integer
For intIdx = 0 To List1.ListCount - 1
If List1.Selected(intInx) Then
List1.List(intIdx) = Replace(Split(strString, " ")(0), "(at)", "@", , , vbTextCompare)
End If
Next
End Sub
Private Sub SaveIt()
'save new file
Dim intFF As Integer, intIdx As Integer
intFF = FreeFile
Open App.Path & "\emails.txt" For Output As #intFF
For intIdx = 0 To List1.ListCount - 1
If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx)
Next
Close #intFF
End Sub
-
Dec 20th, 2005, 10:43 AM
#29
Thread Starter
Fanatic Member
Re: Text Manipulation
That code runs without errors but when I check the file it doesnt save. it does create a file called emails.txt if it doesnt exist but theres nothing in it.
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:44 AM
#30
Re: Text Manipulation
 Originally Posted by Steve_F
That code runs without errors but when I check the file it doesnt save. it does create a file called emails.txt if it doesnt exist but theres nothing in it.
Now that it runs with no errors, lets get back to my question.
Before creating emails.txt, do you, in fact, have something in your listbox?
-
Dec 20th, 2005, 10:45 AM
#31
Thread Starter
Fanatic Member
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 10:55 AM
#32
Re: Text Manipulation
 Originally Posted by Steve_F
Nope
Thats why your text file is empty. Your routine dumps the contents of your listbox into your text file. So, the problem is not with your emails.txt routine, it is with the routine that populates your listbox in the first place.
We are going to step through the process.
I want you to put a command button on your form (it doesn't matter where, it is going to get deleted after we are done anyway).
In the command button, call your LoadIt proceed.
Does your listbox get populated with the contents of load.txt?
If yes, then take LoadIt out of the click event of your command button and call your replace procedure. Does your listbox get repopulated with the stripped down strings.
-
Dec 20th, 2005, 10:59 AM
#33
Thread Starter
Fanatic Member
Re: Text Manipulation
Yes it loads. When I try and replace it doesnt work. The list is empty
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 11:08 AM
#34
Re: Text Manipulation
 Originally Posted by Steve_F
Yes it loads. When I try and replace it doesnt work. The list is empty
Outstanding. We are making progress.
I'm going to play with the replace part of the listbox. I'll be back.
-
Dec 20th, 2005, 11:13 AM
#35
Thread Starter
Fanatic Member
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 11:30 AM
#36
Thread Starter
Fanatic Member
Re: Text Manipulation
Isnt the error in this line
VB Code:
List1.List(intIdx) = replace(Split(strString, " ")(0), "(AT)", "@", , , vbTextCompare)
Shouldnt strString be List1.List(intIdx)??
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 11:41 AM
#37
Re: Text Manipulation
Try this. It took a little dickering around, but I think I have it
VB Code:
On Error Resume Next
Dim strString As String
Dim arrString() As String
Dim i As Long
For i = 0 To List1.ListCount - 1
strString = List1.List(i)
strString = Replace(strString, "(AT)", "@")
arrString = Split(strString, " ")
List1.RemoveItem i
List1.AddItem arrString(0)
Next
List1.Refresh
-
Dec 20th, 2005, 11:47 AM
#38
Thread Starter
Fanatic Member
Re: Text Manipulation
I put that in the doreplace and it doesnt work
C#.net, VB, C++, Java, VS 2005/2008
Dont' forget to rate posts that are helpful to you.
-
Dec 20th, 2005, 11:47 AM
#39
Re: Text Manipulation
 Originally Posted by Steve_F
I put that in the doreplace and it doesnt work
It worked for me. What does it do for you?
-
Dec 20th, 2005, 11:48 AM
#40
Re: Text Manipulation
 Originally Posted by Steve_F
Isnt the error in this line
VB Code:
List1.List(intIdx) = replace(Split(strString, " ")(0), "(AT)", "@", , , vbTextCompare)
Shouldnt strString be List1.List(intIdx)??
Yep, my bad.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|