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. Breaking up strings

Breaking up strings

Scheduled Pinned Locked Moved Visual Basic
question
9 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.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se

    G B 3 Replies Last reply
    0
    • B Brendan Vogt

      Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Use the Regex class with a regular expression pattern like "([a-z])([A-Z])" to find the changes from lower case to upper case. Put a space between them by replacing the occurances using the pattern "$1 $2".

      --- single minded; short sighted; long gone;

      T 1 Reply Last reply
      0
      • B Brendan Vogt

        Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se

        B Offline
        B Offline
        Bassam Saoud
        wrote on last edited by
        #3

        Another way would be to : -Create a character Array of your text using ToCharArray() -Test each character , if Caps add space before the character and add it to the resulting string otherwise add the character to the resulting string:

            Private Function TransFormText(ByVal oText As String) As String
                Dim oResult As String = ""
                Dim oArray() As Char = oText.ToCharArray()
                For Each oItem As Char In oArray
                    If oItem. = oItem Then
                        oResult = oResult & " " & oItem
                    Else
                        oResult = oResult & oItem
                    End If
                Next
                Return Trim(oResult)
            End Function
        
        G 1 Reply Last reply
        0
        • B Brendan Vogt

          Hi, If I have text like: VariantDisplayMode How do I break it up so that it displays as: Variant Display Mode? Basically it is a seperation at the capital letters. Regards ma se

          B Offline
          B Offline
          Bassam Saoud
          wrote on last edited by
          #4

          There was an error in my previous post:

              Private Function TransFormText(ByVal oText As String) As String
                  Dim oResult As String = ""
                  Dim oArray() As Char = oText.ToCharArray()
                  For Each oItem As Char In oArray
                      If Char.ToUpper(oItem) = oItem Then
                          oResult = oResult & " " & oItem
                      Else
                          oResult = oResult & oItem
                      End If
                  Next
                  Return Trim(oResult)
              End Function
          
          1 Reply Last reply
          0
          • B Bassam Saoud

            Another way would be to : -Create a character Array of your text using ToCharArray() -Test each character , if Caps add space before the character and add it to the resulting string otherwise add the character to the resulting string:

                Private Function TransFormText(ByVal oText As String) As String
                    Dim oResult As String = ""
                    Dim oArray() As Char = oText.ToCharArray()
                    For Each oItem As Char In oArray
                        If oItem. = oItem Then
                            oResult = oResult & " " & oItem
                        Else
                            oResult = oResult & oItem
                        End If
                    Next
                    Return Trim(oResult)
                End Function
            
            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.

            --- single minded; short sighted; long gone;

            B 1 Reply Last reply
            0
            • G Guffa

              You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.

              --- single minded; short sighted; long gone;

              B Offline
              B Offline
              Bassam Saoud
              wrote on last edited by
              #6

              Guffa wrote:

              You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.

              sure, But its a good starter.Obviously the poster is a beginner and in such cases its better to provide a solution that is easy to understand and that build up logic...

              G 1 Reply Last reply
              0
              • G Guffa

                Use the Regex class with a regular expression pattern like "([a-z])([A-Z])" to find the changes from lower case to upper case. Put a space between them by replacing the occurances using the pattern "$1 $2".

                --- single minded; short sighted; long gone;

                T Offline
                T Offline
                TwoFaced
                wrote on last edited by
                #7

                Guffa, I think that regular expression would fail to properly parse something like "ThisIsATest" because it wouldn't pick up on two capital letters next to one another. I think something like this would be required to find the word boundaries "([A-Z]|[a-z])(?=[A-Z])". Using a replace of "$1 ".

                G 1 Reply Last reply
                0
                • T TwoFaced

                  Guffa, I think that regular expression would fail to properly parse something like "ThisIsATest" because it wouldn't pick up on two capital letters next to one another. I think something like this would be required to find the word boundaries "([A-Z]|[a-z])(?=[A-Z])". Using a replace of "$1 ".

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  That's a good point. I didn't think of the special case of single letter words.

                  --- single minded; short sighted; long gone;

                  1 Reply Last reply
                  0
                  • B Bassam Saoud

                    Guffa wrote:

                    You should however mention that the method scales very badly, so it's only suitable for very short strings. For a string with 20 characters, it creates 40 string objects to produce the result. Also, for every additional character the memory usage doubles.

                    sure, But its a good starter.Obviously the poster is a beginner and in such cases its better to provide a solution that is easy to understand and that build up logic...

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    Well, at least it increases the traffic to the site when he comes back wondering why the code is getting so f... ehm... extremely slow. ;)

                    --- single minded; short sighted; long gone;

                    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