Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. stringparsing

stringparsing

Scheduled Pinned Locked Moved Visual Basic
csharpc++data-structuresjsonhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephan Wright
    wrote on last edited by
    #1

    Hy everyone! I want to do some stringparsing, but well I seem to having done to much programming in C# and C/C++. So I guess I start mixing things. So what I want to do is the following: I do have an input in an Excel Cell which looks like "xxx-yyyy" or maybe even blanks between them like "xxx - yyy" etc. xxx and yyy represent integers. So now I do want to split this string to get xxx and yyy in seperate integer variables. Well I guess it is quit simple, but at the moment I seem to be mixing things. I know how to do this in C# etc (parsing the array of char) but well... the info I collected from the sheet is stored in a variant variable, because the user should be allowed to either enter "xxx" or "xxx-yyy". Thanks for your help. Stephan.

    S G 2 Replies Last reply
    0
    • S Stephan Wright

      Hy everyone! I want to do some stringparsing, but well I seem to having done to much programming in C# and C/C++. So I guess I start mixing things. So what I want to do is the following: I do have an input in an Excel Cell which looks like "xxx-yyyy" or maybe even blanks between them like "xxx - yyy" etc. xxx and yyy represent integers. So now I do want to split this string to get xxx and yyy in seperate integer variables. Well I guess it is quit simple, but at the moment I seem to be mixing things. I know how to do this in C# etc (parsing the array of char) but well... the info I collected from the sheet is stored in a variant variable, because the user should be allowed to either enter "xxx" or "xxx-yyy". Thanks for your help. Stephan.

      S Offline
      S Offline
      Stephan Wright
      wrote on last edited by
      #2

      I just found out myself. I have to use the Split(string) operation but is there any way to split the string without having blanks to seperate it? If anyone of you knows how to split "123-234" into 123 and 234 (meaning the two integers) then please let me know. At the moment I am able to split "123 - 234" into "123" "-" and "234" but well, if there are no blanks inbetween my programm will crash. And I really would like to avoid this :) Thanks. Stephan.

      S 1 Reply Last reply
      0
      • S Stephan Wright

        Hy everyone! I want to do some stringparsing, but well I seem to having done to much programming in C# and C/C++. So I guess I start mixing things. So what I want to do is the following: I do have an input in an Excel Cell which looks like "xxx-yyyy" or maybe even blanks between them like "xxx - yyy" etc. xxx and yyy represent integers. So now I do want to split this string to get xxx and yyy in seperate integer variables. Well I guess it is quit simple, but at the moment I seem to be mixing things. I know how to do this in C# etc (parsing the array of char) but well... the info I collected from the sheet is stored in a variant variable, because the user should be allowed to either enter "xxx" or "xxx-yyy". Thanks for your help. Stephan.

        G Offline
        G Offline
        Gavin Jeffrey
        wrote on last edited by
        #3

        now that i ponder on it a bit, when i use the split function in c# i simply do the following - myString.split('-')[0] //gets xxx myString.split('-')[1] //gets yyy I wonder if you couldn't do the same for vb.net i.e. myString.split('-')(0) //gets xxx myString.split('-')(1) //gets yyy

        T 1 Reply Last reply
        0
        • G Gavin Jeffrey

          now that i ponder on it a bit, when i use the split function in c# i simply do the following - myString.split('-')[0] //gets xxx myString.split('-')[1] //gets yyy I wonder if you couldn't do the same for vb.net i.e. myString.split('-')(0) //gets xxx myString.split('-')(1) //gets yyy

          T Offline
          T Offline
          The Man from U N C L E
          wrote on last edited by
          #4

          You can but VB.net likes you to specify that the splitter character is of type char. Also the single quote is the comment character in VB.Net so you have to use double quotes. myString.split("-"c)(0) 'gets xxx myString.split("-"c)(1) 'gets yyy If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850)

          1 Reply Last reply
          0
          • S Stephan Wright

            I just found out myself. I have to use the Split(string) operation but is there any way to split the string without having blanks to seperate it? If anyone of you knows how to split "123-234" into 123 and 234 (meaning the two integers) then please let me know. At the moment I am able to split "123 - 234" into "123" "-" and "234" but well, if there are no blanks inbetween my programm will crash. And I really would like to avoid this :) Thanks. Stephan.

            S Offline
            S Offline
            Scott Serl
            wrote on last edited by
            #5

            Why use split? Just use SubString: Dim a As Integer Dim b As Integer Dim s As String = "123 - 456" Dim i As Integer = s.IndexOf("-") If i < 0 Then 'must be in xxx form a = Integer.Parse(s) Else 'in xxx-yyyy form, so parse a = Integer.Parse(s.Substring(0, i)) b = Integer.Parse(s.Substring(i + 1, s.Length - i - 1)) End If This will work if spaces or no spaces. If you need the strings rather than the integers, just use stringa = s.Substring(0, i).Trim. The trim will remove leading or trailing spaces.

            S 1 Reply Last reply
            0
            • S Scott Serl

              Why use split? Just use SubString: Dim a As Integer Dim b As Integer Dim s As String = "123 - 456" Dim i As Integer = s.IndexOf("-") If i < 0 Then 'must be in xxx form a = Integer.Parse(s) Else 'in xxx-yyyy form, so parse a = Integer.Parse(s.Substring(0, i)) b = Integer.Parse(s.Substring(i + 1, s.Length - i - 1)) End If This will work if spaces or no spaces. If you need the strings rather than the integers, just use stringa = s.Substring(0, i).Trim. The trim will remove leading or trailing spaces.

              S Offline
              S Offline
              Stephan Wright
              wrote on last edited by
              #6

              Scott Serl wrote: a = Integer.Parse(s.Substring(0, i)) Well, seams as if VBA for Excel does not support "Integer.Parse" Or does it? At the moment I am on Office 2000 but might soon be updating to Office 2003. I had a look and the help says there is only a normal parse operation. The examples says parsing a telephonenumber and seperating it into two Cells (similar to my example). Maybe I could use this one ... Or was your example supposed for .NET? Because actually I do not have this installed here and at the moment I do not have access to it. So I have to get along with what Office is offering 'til I will be able to use .NET Stephan.

              S 1 Reply Last reply
              0
              • S Stephan Wright

                Scott Serl wrote: a = Integer.Parse(s.Substring(0, i)) Well, seams as if VBA for Excel does not support "Integer.Parse" Or does it? At the moment I am on Office 2000 but might soon be updating to Office 2003. I had a look and the help says there is only a normal parse operation. The examples says parsing a telephonenumber and seperating it into two Cells (similar to my example). Maybe I could use this one ... Or was your example supposed for .NET? Because actually I do not have this installed here and at the moment I do not have access to it. So I have to get along with what Office is offering 'til I will be able to use .NET Stephan.

                S Offline
                S Offline
                Scott Serl
                wrote on last edited by
                #7

                Sorry, In Excel, it would be something like: value = "123 - 456" Pos = Instr(value,"-") If Pos > 0 Then 'I think position starts at 1 in Excel a = Val(Left(value, pos - 1)) b = Val(Right(value, Len(value) - Pos)) Else a = val(value) End If I can't check at this computer because I don't have anything with old vb on it here.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups