String manipulation HELP!!!!!

capcom

Member
Joined
Aug 27, 2006
Messages
11
Programming Experience
5-10
Hope someone can help on this one!

What I have is lines of text in a richtextbox which look like this:
N1G01 F10 X0.5 Y0.0625
N2 Z0.02 F10
N3 Z-0.04 F1
N4 Y1.9375 F2.5
N5 X0.4 F5
N6 Y0.0625
N7 X0.6

What I need to do is find and replace the number right after the X. My problem has been that the number right after the X is not always the same length. Does anyone have an answer for this?

Thank You

Capcom
 
VB.NET:
    Sub findAndReplace(ByVal key As String, ByVal newVal As String)
        Dim str As String

        str = RichTextBox1.Text
        str = Replace(str, key, newVal)

        RichTextBox1.Text = str
    End Sub
Then call this function like this:

VB.NET:
findAndReplace("X0.6", "X0.15")
Now this assumes that you know (or the user) which value they want to change. However if you do not know all the X values in your richtextbox heres how you can retrieve them in a string array. This will return all X<values> disregarding the length.
VB.NET:
Sub getAllXValues()
        Dim str As String
        Dim arr() As String

        str = RichTextBox1.Text
        arr = Split(str, " ") 'It appears your delimiter is a space.

        For i As Integer = 0 To arr.Length - 1
            If arr(i).StartsWith("X") Then
                'Use any of these 3 methods to see your results!
                'Debug.Print(arr(i))
                MsgBox(arr(i))
                'ListBox1.Items.Add(arr(i))
            End If
        Next
End Sub
 
I tried that but it will only change the last line, it won't change line 1 or 5.

N1G01 F10 X0.5 Y0.0625
N2 Z0.02 F10
N3 Z-0.04 F1
N4 Y1.9375 F2.5
N5 X0.4 F5
N6 Y0.0625
N7 X0.6


Thanks

Gary
 
That is weird it works for me, did u try to change the parameters? Such as

Call:
VB.NET:
findAndReplace("X0.[B]5[/B]", "X0.15")

Works here.
 
It don't work on my end.
I can't use findAndReplace("X0.5", "X0.15") because I don't know what the numbers would be.

When I run my program I simply want to click a button and then have a function read line by line and convert the values at the X location from inch to mm.

N1G01 F10 X0.5 Y0.0625
N2 Z0.02 F10
N3 Z-0.04 F1
N4 Y1.9375 F2.5
N5 X0.4 F5
N6 Y0.0625
N7 X0.6
N8 Y1.9375
N9 X0.3 F5
N10 Y0.0625
N11 X0.7

These X and Y values are created after the program is running and they may be over a thousand lines long. I simply want to give the option to convert this over to mm or to inch. They also need to be able to load there own file into this richtextbox and then click the button to convert it over. Some reason I cant do it. The program must read the richtextbox if they are going to be allowed to paste there values into the richtextbox.
Boy I hope that is clear enough.

thanks again for your help

Gary
 
Try to use my getAllXValues sub that I have posted above. It returns all the X values whatever the case is.

Let me know if you need more help. I believe with the current posted code you should be able to work your way through it. Also do you have the function that returns the conversion from inch to mm?

You may also try working from
VB.NET:
dim line as string

for each line in Richtextbox1.lines
'Do your stuff here.
next

I took about 5 minutes and also came up with this. This is NOT complete yet as you have to put the new values back to your rich textbox.

VB.NET:
    Sub findAndReplace()
        Dim str As String
        Dim arr() As String
        Dim tmp As Double

        str = RichTextBox1.Text
        arr = Split(str, " ")

        For i As Integer = 0 To arr.Length - 1
            If arr(i).StartsWith("X") Then
                tmp = arr(i).Substring(1) 'Removes the X
                tmp = convertToMM(tmp) 'Converts it to MM
                arr(i) = "X" & tmp 'Rewrite back to array the converted value
                MsgBox(arr(i)) 'See it to believe it!
            End If
        Next
    End Sub

    Function convertToMM(ByVal number As Double) As Double
        number *= 25.4
        Return number
    End Function
 
Last edited:
Ok, here is something I found.

N1G01 F10 X0.5 Y0.0625
N2 Z0.02 F10
N3 Z-0.04 F1
N4 Y1.9375 F2.5
N5 X0.4 F5
N6 Y0.0625
N7 X0.6
N8 Y1.9375
N9 X0.3 F5
N10 Y0.0625
N11 X0.7

Look at line number N7 it doesn't have a space after it. So it try's to include N8 along with the number X0.6. After the N8 there is a space so that is where it is split. Hope that makes sense.

Maybe it's one of those impossible missions. Hope Not!

Gary
 
Definitely not impossible, we are trying to find the best possible solution here...I hope someone else jumps in.

I will try to work through the N7 problem. Will edit this post when I get something.
 
Because some split is newline char and some space you could best work line by line from the RTB.Lines.

You would also get the same result if you put all the text into temp variable, replaced newlines with space, then searched all X... occurences and replaced them in the original text.

You should also be looking into Regular Expressions which is commonly used in these situations.
 
Ok, might be getting somewhere. I put a space after ever line so that is fixed. Plus I am getting the correct values. Now just how do I replace these values using absolut's sub?

VB.NET:
Sub findAndReplace()
        Dim str As String
        Dim arr() As String
        Dim tmp As Double

        str = RichTextBox1.Text
        arr = Split(str, " ")

        For i As Integer = 0 To arr.Length - 1
            If arr(i).StartsWith("X") Then
                tmp = arr(i).Substring(1) 'Removes the X
                tmp = convertToMM(tmp) 'Converts it to MM
                arr(i) = "X" & tmp 'Rewrite back to array the converted value
                MsgBox(arr(i)) 'See it to believe it!
            End If
        Next
    End Sub
Thanks for all your help

Gary
 
This code works. If you do not have a problem having an extra space to avoid the N7 problem then this is the solution. It works here on both cases I tried!

VB.NET:
    Sub findAndReplace()

        Dim str As String
        Dim arr() As String

        str = RichTextBox1.Text
        arr = Split(str, " ")

        For i As Integer = 0 To arr.Length - 1
            If arr(i).StartsWith("X") Then
                'ListBox1.Items.Add(arr(i)) 'Debug
                str = str.Replace(arr(i), "X" & convertToMM(arr(i).Substring(1)))
            End If
        Next

        RichTextBox1.Text = str

    End Sub

    Function convertToMM(ByVal number As Double) As Double
        number *= 25.4
        Return number
    End Function
There might be more efficient way but this should be good. Try with a huge file and let me know!
 
Sorry nogo. This is what I get when I run it:

N1G01 F10 X1.41951794524489E+15 Y0.0625
N2 Z0.02 F10
N3 Z-0.04 F1
N4 Y1.9375 F2.5
N5 X1.1356143561959E+15 F5
N6 Y0.0625
N7 X1.70342153429387E+15
N8 Y1.9375
N9 X851710767146931 F5
N10 Y0.0625
N11 X1.98732512334284E+15

The thing of it is, if I just run the very first line it replaces the X value properly, but if I add line 2 then it gives those weird numbers.

Gary
 
Capcom, I have it in front of me and it works fine. Did you copy the code exactly as its posted?

BEFORE --> AFTER
 

Attachments

  • before.JPG
    before.JPG
    19.1 KB · Views: 61
  • after.JPG
    after.JPG
    20.7 KB · Views: 59
Back
Top