Results 1 to 12 of 12

Thread: add a space to every other character [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    add a space to every other character [RESOLVED]

    what's the best way to add a space every other character in a text file? The end result I want is to take this: 123456 7890 and turn it into this: 1 2 3 4 5 6 7 8 9 0. This also adds a space to a space.

    any ideas?
    Last edited by Andy; Feb 11th, 2004 at 09:28 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's one way ,
    VB Code:
    1. Dim str1 As String = "1234567890"
    2.         Dim str2 As String
    3.         str1.ToCharArray()
    4.         For i As Integer = 0 To str1.Length
    5.             str2 += Str(i)
    6.         Next
    7.         MessageBox.Show(str2)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    all that code does is replace text with a sequential number run. that's on the track though. I need to take ANY typed text and space it out.

    andy becomes a n d y

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    so far, I have made a function that uses a select case and compares the alphabet like this:


    "s" becomes "s "

    how can I use the streamreader to send each character to the bunction?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I came up with this instead of the function:


    Code:
    Dim length As Integer = txtMessage.Text.Length
            Dim character As Integer
            For character = 0 To length Step 2
                txtMessage.Text = txtMessage.Text.Insert(character, " ")
            Next
    but it only does HALF of the text because of the step 2 I guess.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Code:
     'put a space betweeen every other character
            Dim length As Integer = txtMessage.Text.Length
            Dim character As Integer = 0
            For character = 0 To (length * 2) Step 2
                txtMessage.Text = txtMessage.Text.Insert(character, " ")
            Next
    this is what I'm after. Can't believe I figured it out by myself!!!

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could also do this:
    VB Code:
    1. 'put a space betweeen every other character
    2.         Dim length As Integer = txtMessage.Text.Length
    3.         Dim character As Integer = 0
    4.         For character = length To 0 Step -1
    5.             txtMessage.Text = txtMessage.Text.Insert(character, " ")
    6.         Next

    EDIT: That was my bad you'd have to step through backwards to use length.
    Last edited by Edneeis; Feb 11th, 2004 at 11:12 AM.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Originally posted by Edneeis
    You could also do this:
    VB Code:
    1. 'put a space betweeen every other character
    2.         Dim length As Integer = txtMessage.Text.Length
    3.         Dim character As Integer = 0
    4.         For character = 0 To length
    5.             txtMessage.Text = txtMessage.Text.Insert(character, " ")
    6.         Next

    that's actualy what I had at first I think but it doesn't skip every other character. it places spaces at the beginning of the text equal to the length.

    I had it to where it would do exactly HALF the text and then, just as a shot-in-the-dark, decided to double the LENGTH variable. It worked. I only wish I figured that out in my head instead of playing battleship with it LOL

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by thephantom
    all that code does is replace text with a sequential number run. that's on the track though. I need to take ANY typed text and space it out.
    andy becomes a n d y
    I took it you want to do that with numbers . sry

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "

    After watching you guys run round in circles are you sure you have resolved the original question? I think the code you finished up with still puts a space after a space, which at one stage was not acceptable.

    Try:

    Dim str1 As String = "12345 67890"
    Dim str2 As String
    Dim x, y As Integer
    y = 1
    str2 = ""
    For x = 0 To Len(str1) - 1
    If Mid(str1, y, 1) <> " " Then
    str2 += Mid(str1, y, 1) + " "
    End If
    y = y + 1
    Next
    MessageBox.Show(str2)
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    oops!! I closed this thread as resolved and forgot to post my code at how it got resolved!!!!


    VB Code:
    1. 'put a space betweeen every other character
    2.         Dim length As Integer = txtMessage.Text.Length
    3.         Dim character As Integer = 0
    4.  
    5.         'space-out the message
    6.         For character = 0 To (length * 2) Step 2
    7.             txtMessage.Text = txtMessage.Text.Insert(character, " ")
    8.         Next

    this DOES put a space after a space, but that's necessary to still be able to read the string.

    the purpose of this code was to take an email message, space the strings and by-pass a spam-guard program that was falsely rejecting my emails. works like a charm!!!

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "'put a space betweeen every other character
    Dim length As Integer = txtMessage.Text.Length
    Dim character As Integer = 0

    'space-out the message
    For character = 0 To (length * 2) Step 2
    txtMessage.Text = txtMessage.Text.Insert(character, " ")
    Next"




    If you do not want leading or trailing spaces


    For character = 1 To (length * 2 - 2) Step 2
    txt2.Text = txt2.Text.Insert(character, " ")
    Next
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width