Space in textBox

  • Thread starter Thread starter *.dpG
  • Start date Start date
D

*.dpG

Hello
I have problem with this... In textBox I have some words with space
between for example
asdf fhhhh jjjjjkkkkk oooooo
and space is not same between all word. I try to have only word in
listBox without space (or in new array) and i try this:
----------------------------------------------------------------------------
Dim stringNeki () As String = textBox.Split(" ")
For Each stringN As String In stringNeki
If stringN <> " " Then
lstBox.Items.Add(stringN)
End If
Next
but dosen't work....

Can anybody help me, how to solve this.....

ThnX....
 
First replace all double spaces with single spaces like

s= textbox.text.replace(" ", " ")

you may have to loop a number of times like

s=textbox.text

For i - 0 to 10
s=s.replace(" ", " ")
Next

Then your method will remove the remaining single space

hth,
Samuel Shulman
 
The Split method works on a String, not a Textbox. Also, the .Split(" ")
will cause all the spaces to be stripped out completely, so you don't want
to test for <> " ", you want to test for <> "". The following code works
for me:

Dim stringNeki () As String() = textBox.Text.Split(" ")
For Each stringN As String In stringNeki
If stringN <> "" Then
lstBox.Items.Add(stringN)
End If
Next
 
Hello *.dbG, hello Samuel,

I would use this approach:

Dim s As String

s = TextBox1.Text

While Instr(s, " ") > 0
s = Replace(s, " ", "")
End While

TextBox1.Text = s

Best Regards,

HKSHK
 
That doesn't solve the problem that the OP posed. What you've shown will
simply strip out all the spaces and create 1 resulting string. The OP is
looking for many text values that were separated by one or more strings.
This does the trick:

Dim stringNeki () As String() = textBox.Text.Split(" ")
For Each stringN As String In stringNeki
If stringN <> "" Then
lstBox.Items.Add(stringN)
End If
Next
 
Back
Top