10 Replies - 28163 Views - Last Post: 04 January 2006 - 11:48 AM Rate Topic: -----

#1 gm2777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 05

String manipulation VB.net

Posted 31 December 2005 - 07:44 AM

Could someone give me a helping hand? What I have is a text file that I am reading one line at a time. Here is one of the line's that I am reading "N2970 G00 X5.1818 Y4.7868". What I want is to extract the number that is right after the X. How do you do such a thing?

Thank you very much

Gary
Is This A Good Question/Topic? 0
  • +

Replies To: String manipulation VB.net

#2 Xenon  Icon User is offline

  • Connection failed
  • member icon

Reputation: 8
  • View blog
  • Posts: 1,595
  • Joined: 12-September 05

Re: String manipulation VB.net

Posted 31 December 2005 - 08:06 AM

i think you should refer to this Link

as far as i think, you should use a for loop and then use the .SubString method to extract ONe character at a time. then you should compare that character to "X", if it is "X" then, use the .SubString method again to extract the numbers.

about For loop

The For loop in VB .NET needs a loop index which counts the number of loop iterations as the loop executes. The syntax for the For loop looks like this:
For index=start to end[Step step]
[statements]
[Exit For]
[statements]
Next[index] 



The index variable is set to start automatically when the loop starts. Each time in the loop, index is incremented by step and when index equals end, the loop ends.

Example on For loop
Module Module1

Sub Main()
Dim d As Integer
For d = 0 To 2
System.Console.WriteLine("In the For Loop")
Next d
End Sub

End Module  




You should get something outta this :) !
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String manipulation VB.net

Posted 31 December 2005 - 08:26 AM

If the X is always in the same spot, you can directly access using the substring method, as Xenon mentioned:
Dim str1 As String
Dim str2 As String
str1 = "N2970 G00 X5.1818 Y4.7868"
str2 = str1.Substring(11, 1)


str2 carries the value of the 5

if you need to find the X, use the indexof method.
Dim str1 As String
Dim str2 As String
str1 = "N2970 G00 X5.1818 Y4.7868"
num = str1.IndexOf("X")
str2 = str1.Substring(num + 1, 1)


Of course, this code is not optimized, but done in the long version to illustrate the techniques.
Was This Post Helpful? 0
  • +
  • -

#4 gm2777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 05

Re: String manipulation VB.net

Posted 31 December 2005 - 08:30 AM

Thank You guy's for the fast reply, I will give your idea's a try.

Gary
Was This Post Helpful? 0
  • +
  • -

#5 gm2777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 05

Re: String manipulation VB.net

Posted 02 January 2006 - 07:10 AM

Ok guy's I still have a problem. Amadeus your code worked, but the problem I am having is the number's are not always the same length, for instance. "N50 g02 X2.72 Y2.5061 I0.0098 J0.0". If you notice the X is 3 number's, counting the dot it is 4. Now the J is 0.0. Trying to read all of these different sized number's is not working at all. If they were all the same it would be different, but they are not.

Thank's for you help in this.

Gary
Was This Post Helpful? 0
  • +
  • -

#6 Xenon  Icon User is offline

  • Connection failed
  • member icon

Reputation: 8
  • View blog
  • Posts: 1,595
  • Joined: 12-September 05

Re: String manipulation VB.net

Posted 02 January 2006 - 07:54 AM

Just for curiousity's sake wher do these numbers get generated from ?

i have just an idea, that if your numbers length have a maximum value
say 7 digits.. then you could add "0" after the last decimal digit.. so that the length of the number is always fixed.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String manipulation VB.net

Posted 02 January 2006 - 08:42 AM

gm2777, on 2 Jan, 2006 - 10:07 AM, said:

Ok guy's I still have a problem. Amadeus your code worked, but the problem I am having is the number's are not always the same length, for instance. "N50 g02 X2.72 Y2.5061 I0.0098 J0.0". If you notice the X is 3 number's, counting the dot it is 4. Now the J is 0.0. Trying to read all of these different sized number's is not working at all. If they were all the same it would be different, but they are not.

Thank's for you help in this.

Gary

Are they always the same type of format? y this, I mean do they contain an X with a number after it, and then a space? With a space before the X? If so, you can use the split method to break the string into an arrays of strings...take the element that begins with X, strip the X, and what remains is the number.

Again, the code is not optimized, but will return just the number after the X:
        Dim str1 As String
        Dim block As String
        Dim result As String
        Dim str2 As String()
        str1 = "N50 g02 X2.72 Y2.5061 I0.0098 J0.0"
        str2 = str1.Split(" ")
        For Each block In str2
            If block.Substring(0, 1) = "X" Then
                result = block.Substring(1, block.Length - 1)
            End If
        Next
        Label1.Text = result


Was This Post Helpful? 0
  • +
  • -

#8 gm2777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 05

Re: String manipulation VB.net

Posted 02 January 2006 - 10:34 AM

Here is what I get as an error.

Quote

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index and length must refer to a location within the string.


Now here is the function I wrote with your code! The code that caused the error is the Substring(0,1).
Function ParseIT2(ByVal str1 As String, ByVal lookfor As String)
        'Dim str1 As String
        Dim block As String
        Dim result As String
        Dim str2 As String()
        str2 = str1.Split(" ")
        For Each block In str2
            If block.Substring(0, 1) = lookfor Then
           ¬†result = block.Substring(1, block.Length - 1)
            End If
        Next
        ParseIT2 = result
    End Function



Now here is more of the File I am reading.

N10 G00 Z5 M09
N20 G00 X-5 Y96.5 T1
N30 G01 Z-40 F200 S1000 M03 M08
N40 G01 X-0.571 F700
N50 G01 X-0.657 Y96.414
N60 G03 X-1.79 Y88 I4.95 J-4.95
N70G01X-5
N80G01Y79.5
N90G01X0
N100G01X14.858Y71



You see the format may not be the same, there may be spacing there may not be. If there is an X, or Y, or Z, or I, or J on a line it does have a number after it.
So you see I have to be able to read whatever that number is after any letter, whatever that letter or number may be.

Thanks again for listening!

Gary

This post has been edited by gm2777: 02 January 2006 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String manipulation VB.net

Posted 02 January 2006 - 11:11 AM

In that case, you're going to have to go back to a modified version of Xenon's original suggestion. You will have to traverse the string character by character (or at least use the IndexOf method to find each of that characters you are looking for)...once you locate one of the trigger characters, you will have to check each successive character to see if it is a number (keeping in mind you may need decimals), and build a second string.

Alternately (and this may be easier), you can run a loop that that takes in a line into a variable. Then replace every non decimal non digit character in the variable with a space (and further replace multiple spaces with a single space). Trim the leading and trailing spaces, you'll be left with a string holding only the number values, each separated by a space.
Was This Post Helpful? 0
  • +
  • -

#10 gm2777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 05

Re: String manipulation VB.net

Posted 04 January 2006 - 11:37 AM

I would like to thank everyone who has helped me. I think I have finally got a function to do what I want.

I would like to ask another simple question if I may.

Here is the code to read a file. I am trying to delete a line completly by using this (sTemp = "" ), but it does not delete the line from the file. What method would work?



FileOpen(HFILE, SMYSOURCEFILE, OpenMode.Input)
        Do While Not EOF(HFILE)   ' Loop until end of file.
            sTemp = LineInput(HFILE)   ' Read line into variable.
            If sTemp.StartsWith("(") Then
                sTemp = "" 
            End If
            sBuf(Lineinc) = sTemp
            Lineinc += 1
        Loop


Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String manipulation VB.net

Posted 04 January 2006 - 11:48 AM

Are you writing the text back to the file? If so, where is the defintion for sBuf?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1