Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i have a program that contains the current text in its textbox:

Blaaaat
Blaaat
Blaat

 

Now another program accesses this textbox and reads it out, but some problems occur, with hidden characters messing up some things:

        aSplitted = strTextBuff.Split(Environment.NewLine)
       For Each aElement As String In aSplitted
           Debug.WriteLine(aElement.Length & " - " & aElement)
       Next

 

the output of this debug is:

7 - Blaaaat
7 - 
Blaaat
7 - 
Blaat

 

Not exactly what you would expect, this is what i was expecting:

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

  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

  • Administrators
Posted

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

aSplitted() As String = System.Text.RegularExpressions.Regex.Split(strTextBuff, Environment.NewLine)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posting Guidelines

 

Avatar by Lebb

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...