ggo6

Ok, so say Im trying to make a program that removes the "space" charachters in a char array. I can get my program to replace all the spaces with a "Nothing" value, but when I try printing it to a string or a textbox, it only prints up to the first "nothing".

Eg: The original char array spells out "What's Your Name ", I edit the values so it now holds "What'sYourName " (with a nothing value replacing the spaces), but when I try to print the char array it only says, "What's".

Also, Im not actually writing a program that removes spaces (Which wouldnt be very useful). Im making a program that encodes text, and removing spaces is part of the encoding procedure. While I could simply remove this part of the procedure (since Im making this for fun anyway), Im still curious about how I can fix the problem.

Thanks to anyone who helps!



Re: Visual Basic Language Storing a Char array in a string

Spidermans_DarkSide - MSP, VSIP

ggo6 wrote:

Ok, so say Im trying to make a program that removes the "space" charachters in a char array. I can get my program to replace all the spaces with a "Nothing" value, but when I try printing it to a string or a textbox, it only prints up to the first "nothing".

Eg: The original char array spells out "What's Your Name ", I edit the values so it now holds "What'sYourName " (with a nothing value replacing the spaces), but when I try to print the char array it only says, "What's".

Also, Im not actually writing a program that removes spaces (Which wouldnt be very useful). Im making a program that encodes text, and removing spaces is part of the encoding procedure. While I could simply remove this part of the procedure (since Im making this for fun anyway), Im still curious about how I can fix the problem.

Thanks to anyone who helps!

Hi,

Try this. You don't need any control like a button.>>

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Create a string.

Dim myString As String = "What's your name."

' "index" is used in the FOR NEXT loop.

Dim index As Integer

'Create a CHARacter array as the same

'number of CHAR's as the string "myString" above.

Dim myChars(myString.Length - 1) As Char

For index = 0 To myString.Length - 1

'Put the characters into the array.

myChars(index) = CChar(myString.Substring(index, 1))

Next

'Replace the spaces with a "-"

For index = 0 To myString.Length - 1

If myChars(index) = " " Then myChars(index) = "-"

Next

'Create the "newString".

Dim newString As String = ""

For index = 0 To myString.Length - 1

newString += myChars(index)

Next

'Output the "newString".

MessageBox.Show(" 'newString' is now= " & newString)

End Sub

End Class






Re: Visual Basic Language Storing a Char array in a string

kleinma

If you have a char array, simply create a string from it, call the replace method, and convert the string back to a char array. Its only one line of code:

Code Snippet

Dim MyCharArray As Char() = "What's Your Name ".ToCharArray

MyCharArray = New String(MyCharArray).Replace(" ", "").ToCharArray






Re: Visual Basic Language Storing a Char array in a string

ggo6

Thanks, but the problem isnt storing it, its displaying it on a textbox (or putting it into a string) which I dont need. I think what happens is when VB sees a nothing value, it automatically assumes the char array is over.

For now I think I''ll stick with the first suggestion, and settle for replacing spaces with an other charachter. Thanks again.