Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:07:55+00:00 2026-05-14T05:07:55+00:00

I was experimenting with basic VB.Net File IO and String splitting. I encountered this

  • 0

I was experimenting with basic VB.Net File IO and String splitting. I encountered this problem. I don’t know whether it has something to do with the File IO or String splitting.

I am writing text to a file like so

Dim sWriter As New StreamWriter("Data.txt")
sWriter.WriteLine("FirstItem")
sWriter.WriteLine("SecondItem")
sWriter.WriteLine("ThirdItem")
sWriter.Close()

Then, I am reading the text from the file

Dim sReader As New StreamReader("Data.txt")
Dim fileContents As String = sReader.ReadToEnd()
sReader.Close()

Now, I am splitting fileContents using Environment.NewLine as the delimiter.

Dim tempStr() As String = fileContents.Split(Environment.NewLine)

When I print the resulting Array, I get some weird results

For Each str As String In tempStr
  Console.WriteLine("*" + str + "*")
Next

I added the *s to the beginning and end of the Array items during printing, to find out what is going on. Since NewLine is used as the delimiter, I expected the strings in the Array to NOT have any NewLine’s. But the output was this –

*FirstItem*
*
SecondItem*
*
ThirdItem*
*
*

Shouldn’t it be this –

*FirstItem*
*SecondItem*
*ThirdItem*
**

??

Why is there a new line in the beginning of all but the first string?

Update: I did a character by character print of fileContents and got this –

F - 70
i - 105
r - 114
s - 115
t - 116
I - 73
t - 116
e - 101
m - 109
 - 13

 - 10
S - 83
e - 101
c - 99
o - 111
n - 110
d - 100
I - 73
t - 116
e - 101
m - 109
 - 13

 - 10
T - 84
h - 104
i - 105
r - 114
d - 100
I - 73
t - 116
e - 101
m - 109
 - 13

 - 10

It seems ‘Environment.NewLine’ consists of

 - 13

 - 10

13 and 10.. I understand. But the empty space in between? I don’t know whether it is coming due to printing to the console or is really a part of NewLine.

So, when splitting, only the character equivalent of ASCII value 13, which is the first character of NewLine, is used as delimiter (as explained in the replies) and the remaining stuff is still present in the strings. For some reason, the mysterious empty space in the list above and ASCII value 10 together result in a new line being printed.

Now it is clear. Thanks for the help. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:07:55+00:00Added an answer on May 14, 2026 at 5:07 am

    First of all, yes, WriteLine tacks on a newline to the end of the string, hence the blank line at the end.

    The problem is the way you’re calling fileContents.Split(). The only version of that function that takes only one argument takes a char(), not a string. Environment.NewLine is a string, not a char, so (assuming you have Option Strict Off) when you’re calling the function it’s implicitly converting it to a char, using only the first character in the string. This means that instead of splitting your string on the actual sequence of two characters that make up Environment.NewLine, it’s actually splitting only on the first of those characters.

    To get your desired output, you need to call it like this:

    Dim delims() as String = { Environment.NewLine }
    Dim tempStr() As String = fileContents.Split(delims, _
                              StringSplitOptions.RemoveEmptyEntries)
    

    This will cause it to split on the actual string, rather than the first character as it’s doing now, and it will remove any blank entries from the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When you have strings as properties, declare them as copy,… May 16, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer The "best practice" (am I really the only one who… May 16, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer Per the Haml documentation, interpolation within filters is normal and… May 16, 2026 at 6:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've been experimenting with ASP.NET MVC and following this tutorial to create the basic
I'm experimenting a bit with textual comparison/basic plagiarism detection, and want to try this
I am experiencing a strange problem with asp.net forms authentication. This problem only occurs
I've been experimenting with Rhino Mocks for unit testing my .Net Compact Framework application,
I am a systems programmer, so i just know some basic css/html. I like
I experimenting with writing Vista/W7 gadgets. In my experiment I want to write the
I've just started experimenting with CodeContracts in .NET 4 on an existing medium-sized project,
I am experiencing a problem with IE caching the results of an action method.
I’ve got a situation with a large internal corporate web based application running ASP.NET
I've been messing around with the canvas element in html5, and this is what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.