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. replace all caracters in a string that are not one of 0-9 or a-z or A-Z with a space...

replace all caracters in a string that are not one of 0-9 or a-z or A-Z with a space...

Scheduled Pinned Locked Moved Visual Basic
questionregex
3 Posts 3 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
    Support123
    wrote on last edited by
    #1

    hi all, How would one go about replacing a character in a string with a space where the character is not in the range of a-z or A-Z or 0-9 ??? the following Code did not have the planned outcome... Dim cleanString As String = [String].Empty Dim reg As New Text.RegularExpressions.Regex("[A-Z]|[a-z]|[0-9]") Dim coll As Text.RegularExpressions.MatchCollection = reg.Matches(LineText, ) For i As Integer = 0 To LineText.Length - 1 MsgBox(coll(i).Value) If (coll(i).Value = [String].Empty) Then cleanString = cleanString + " " Else cleanString = cleanString + coll(i).Value End If Next the result was not pritty.... it removed spaces as well... how do i keep spaces and replace (NOT REMOVE) other characters with a space??? Thank you in advance.

    "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

    S G 2 Replies Last reply
    0
    • S Support123

      hi all, How would one go about replacing a character in a string with a space where the character is not in the range of a-z or A-Z or 0-9 ??? the following Code did not have the planned outcome... Dim cleanString As String = [String].Empty Dim reg As New Text.RegularExpressions.Regex("[A-Z]|[a-z]|[0-9]") Dim coll As Text.RegularExpressions.MatchCollection = reg.Matches(LineText, ) For i As Integer = 0 To LineText.Length - 1 MsgBox(coll(i).Value) If (coll(i).Value = [String].Empty) Then cleanString = cleanString + " " Else cleanString = cleanString + coll(i).Value End If Next the result was not pritty.... it removed spaces as well... how do i keep spaces and replace (NOT REMOVE) other characters with a space??? Thank you in advance.

      "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      Try using the Regex.Replace[^] function.

      Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


      [Forum Guidelines] [Articles] [Blog]

      1 Reply Last reply
      0
      • S Support123

        hi all, How would one go about replacing a character in a string with a space where the character is not in the range of a-z or A-Z or 0-9 ??? the following Code did not have the planned outcome... Dim cleanString As String = [String].Empty Dim reg As New Text.RegularExpressions.Regex("[A-Z]|[a-z]|[0-9]") Dim coll As Text.RegularExpressions.MatchCollection = reg.Matches(LineText, ) For i As Integer = 0 To LineText.Length - 1 MsgBox(coll(i).Value) If (coll(i).Value = [String].Empty) Then cleanString = cleanString + " " Else cleanString = cleanString + coll(i).Value End If Next the result was not pritty.... it removed spaces as well... how do i keep spaces and replace (NOT REMOVE) other characters with a space??? Thank you in advance.

        "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison

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

        With a single line of code: Dim cleanString = Regex.Replace(LineText, "[^A-Za-z\d ]", " ")

        Despite everything, the person most likely to be fooling you next is yourself.

        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