Unknown chars in string

Ontani

Centurion
Joined
Mar 3, 2004
Messages
121
Location
Belgium
i have a program that contains the current text in its textbox:
Code:
Blaaaat
Blaaat
Blaat

Now another program accesses this textbox and reads it out, but some problems occur, with hidden characters messing up some things:
Code:
        aSplitted = strTextBuff.Split(Environment.NewLine)
        For Each aElement As String In aSplitted
            Debug.WriteLine(aElement.Length & " - " & aElement)
        Next

the output of this debug is:
Code:
7 - Blaaaat
7 - 
Blaaat
7 - 
Blaat

Not exactly what you would expect, this is what i was expecting:
Code:
7 - Blaaaat
6 - Blaaat
5 - Blaat

How can i prevent this? What is the problem here? I tried replacing all chrs from 0 to 31 without any success.

Anyone an idea?

This could be very simple
 
How is the text getting into the textbox? Is this being typed in or has it been entered in the designer?
If you take the length of the textbox's contents does it reflect the visible text or the mixture of visible and non visible characters?
 
The text is grabbed from notepad at this point, this will be in a later stadium a game console.

The length of the text is 23 while there are only 18 characters.
So Non-Visible characters are included. 20 or 21 characters would be normal (2 or 3 linebreaks) but 23 is just strange.
 
I always get caught out by this one myself - the String.Split only splits on characters not strings and Environent.NewLine is two characters long. Effectively it means you are getting the line feed part of the carriage return+line feed combination left over.

One alternative is to use a simple regular expression - the following should work
Visual Basic:
aSplitted() As String = System.Text.RegularExpressions.Regex.Split(strTextBuff, Environment.NewLine)
 
That seemed to be the problem, Thank you.

Now when i don't press an enter after the last line in notepad anything that follows is discarded, maybe its an EOF character or something like that.
 
You could also try the 'Microsoft.VisualBasic.Strings.Split' method, which can also handle a multi-character string splitter. It might handle your "last character" issue better, don't know...

I think it's also a little safer to use in general than RegEx.Split(), unless you really do wish to split on a pattern. In your case you are passing in Cr & Lf, so it's fine, but if you passed in "\d", for example, all heck would break loose, splitting on every digit... You know the source in this case, so it's ok, but if you don't know what the spit value will be beforehand (supplied by the user, perhaps?) then you could get really snagged with RegEx.Split, I think.
 
Thanks for the heads up, but as you said the source of the split char is know and not defined by any user input.
 
Back
Top