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
  1. Home
  2. General Programming
  3. Visual Basic
  4. Variation on string manipulation

Variation on string manipulation

Scheduled Pinned Locked Moved Visual Basic
tutorialhelp
6 Posts 5 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.
  • B Offline
    B Offline
    BluesEnd
    wrote on last edited by
    #1

    Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve

    Thanks for your help and advice! Steve

    L S H 3 Replies Last reply
    0
    • B BluesEnd

      Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve

      Thanks for your help and advice! Steve

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I'm not going to give you any solution; I do suggest you try and solve this little task in several steps, it is a typical beginner's mistake to try and solve it in one complex statement (which often never gets composed correctly). With many statements, you can execute the required steps, similar to your explanation in English words, AND you can check intermediate results. So don't worry if you use ten lines of code for this; once you got it working correctly, you can try and optimize it. One hint: you don't need loops, a few simple string methods will do. :)

      Luc Pattyn


      Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


      Local announcement (Antwerp region): Lange Wapper? Neen!


      1 Reply Last reply
      0
      • B BluesEnd

        Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve

        Thanks for your help and advice! Steve

        S Offline
        S Offline
        Steven J Jowett
        wrote on last edited by
        #3

        You will could use the following methods of your string variable, to achieve your requirements :-

        .Length
        .Substring
        .Replace

        like so:-

        Dim FullString = "string of 23 characters"
        Dim FirstPart As String
        Dim SecondPart As String

        FirstPart = FullString
        SecondPart = FullString.Substring(FullString.Length /2)
        FirstPart = Replace(FirstPart, SecondPart, "")

        Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

        1 Reply Last reply
        0
        • B BluesEnd

          Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve

          Thanks for your help and advice! Steve

          H Offline
          H Offline
          Hurricane3000
          wrote on last edited by
          #4

          I think is this the code you need:

          Dim FullString As String = "String of 23 characters"
          Dim HalfStringLen As Integer = Fix(FullString.Length / 2)
          Dim FirstPart As String = FullString
          Dim SecondPart As String = ""
          Do
          If FullString(HalfStringLen) = " " Then
          FirstPart = Microsoft.VisualBasic.Left(FullString, HalfStringLen)
          SecondPart = LTrim(Microsoft.VisualBasic.Right(FullString, FullString.Length - HalfStringLen))
          Exit Do
          End If
          HalfStringLen = HalfStringLen + 1
          Loop Until HalfStringLen = FullString.Length

          At end of loop, FirstPart and SecondPart will contain FullString spitted. Note that If the full string have not espaces, FirstPart will contain the entire FullString. Ignazio

          B 1 Reply Last reply
          0
          • H Hurricane3000

            I think is this the code you need:

            Dim FullString As String = "String of 23 characters"
            Dim HalfStringLen As Integer = Fix(FullString.Length / 2)
            Dim FirstPart As String = FullString
            Dim SecondPart As String = ""
            Do
            If FullString(HalfStringLen) = " " Then
            FirstPart = Microsoft.VisualBasic.Left(FullString, HalfStringLen)
            SecondPart = LTrim(Microsoft.VisualBasic.Right(FullString, FullString.Length - HalfStringLen))
            Exit Do
            End If
            HalfStringLen = HalfStringLen + 1
            Loop Until HalfStringLen = FullString.Length

            At end of loop, FirstPart and SecondPart will contain FullString spitted. Note that If the full string have not espaces, FirstPart will contain the entire FullString. Ignazio

            B Offline
            B Offline
            BluesEnd
            wrote on last edited by
            #5

            Hi Ignazio and Steve, Thanks for your replies, they have both helped me a lot and it's helped me to solve my problem. again, thanks Steve

            Thanks for your help and advice! Steve

            D 1 Reply Last reply
            0
            • B BluesEnd

              Hi Ignazio and Steve, Thanks for your replies, they have both helped me a lot and it's helped me to solve my problem. again, thanks Steve

              Thanks for your help and advice! Steve

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Congratulations! You managed to con someone into doing your work for you! Don't do it again.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              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