I have this file that I read from. It reads each line as a seperate string. I want to be able to make two lines from it.
Hmm that didnt make much sense.
Say I have the following line in a text file.
"This is a line. This is a newline"
I want that line to display in a textbox like so:
This is a line.
This is a newline.
I don't want to use a split function on that line. I just don't. Is there an escape character or something I could use A newline character or anything that I could use
Thank you,
Troy L

How to make a newline
MarcZhou
I'd agree with this statement. Whether you call it a delimter or a control character - its some character or string of characters which you can identify where to make the change.
You can put anything in the string which would be unlikey to be used in the actual text such as [\n] and then once you have the line do a replace on this set of character with environment.newline
dim s as string = "this is [\n] a test"
msgbox(s.replace("[\n]","") '<- One line
msgbox(s.replace("[\n]",Environment.Newline) '<- two lines
the [\n] string can be anything you want but make it something that is rather unlikely to appear in normal string context.
Le Saint
Hi,
I believe you're looking for Environment.NewLine:
Dim myText As String = "This is a line." & Environment.NewLine & "This is a newline"There's also Chr(13) & Chr(10) combo:
Dim myText As String = "This is a line." & Chr(13) & Chr(10) & "This is a newline"... but Environment.NewLine is more platform independent [other than syntax, it's the same thing when running on Windows].
One thing that's not clear to me is how are you going to insert this into your one-line string, when reading from file... You wrote you don't want to use the split function... How about replacing a specific part of the string with the NewLine chars
Dim myNewText As String = myText.Replace(". ", "." & Environment.NewLine)Or... Perhaps you want to insert that new line character escaper into your text file
Andrej
tehbagel
Well, you can put a CRLF in the original line (CHR(10) + CHR(13) or ControlChars.NewLine) and that will make it two lines... But you'll have to modify the source text file. Is that what you want to do
I really don't understand the question... I mean, I guess you could look for a period followed by a space and then insert the newline char if that's what you need:
Dim str As String = "This is a line. This is a new line."
str = str.Insert(str.IndexOf(". ") + 2, ControlChars.NewLine)
Me.TextBox1.Text = str
That would cause the line to appear on two lines in a multiline textbox without splitting it into two strings. But you could have issues ensuring that you insert the newline char in the right place everytime. If the file you are reading is consistantly formatted than you might get away with it.
AndersBank
just to add, why wouldnt you want to use a split function it can make things easier in this case. Here is a sample anyway just for your reference:
Dim theText() as String = theInput.Split(new Char() {"."})
this would split at every period it came across, and store each element found in this criteria into the string array
Kennon2005
Ok, that makes much more sense.
Now, if you can find any consistancy to the file, you can use the code I posted:
Dim str As String = "This is a line. This is a new line."
str = str.Insert(str.IndexOf(". ") + 2, ControlChars.NewLine)
Me.TextBox1.Text = str
For instance if you can say that a rule is "the first space always seperates the lines" then the code can be changed to:
str = str.Insert(str.IndexOf(" ") + 1, ControlChars.NewLine)
Inserting the NewLine char will work so long as you can determine where in the string to insert it.
Martin2233
I have a file that has 9 strings, each on it's own line. Also, each string has colons in it.
So...
line 1:word 2
line 2:word 2
line 3:word 2
line 4:word 2
line 5:word 2
line 6:word 2
line 7:word 2
line 8:word 2
line 9:word 2
I use a split function to split the file by line using vbCrLf as the delimiter. These strings get input to SplitFile as String()
I use another split function to split each line. It takes each string in SplitFile and splits it using a colon as a delimiter.
So Spliting the first string would give me two strings, line 1 and word 2.
Now say I want to split up one of the two string, but not use a delimiter.
so instead of filling a textbox with word 2 it would fill it with
word
2
I am basically looking for a newline char that would work. Like \n or something.
I guess I am out of luck if nobody can understand my ranting.
The Markus
I'd say this one is the confusing part... If you want to split a string, you'd have to know where to split it (= delimiter)
Andrej
VoiceOfExperience
if you have "word 2" then you just split the space in between it, once split into the string array then pretty much you have your lines of text there, word and 2, in each of its own place in the string array. Then you can go through each element in the string array and just put the text into the textbox for example (which accepts multilines) and making sure that ever each read of the element in the array to use Environment.NewLine
another way would be to go through each character and see if the current character is a space (VBCRLF ) and if it is then take everything form this point and place it on a new line, using the substring method or something
so we had:
Dim theWordToSplit as String = "word 2"
Dim theIndex as Integer = 0
for each currentChar as string in theWordToSplit
if currentChar.ToString().Equals(" ") then
Dim theFirstWord as String = theWordToSplit.SubString(theIndex, theWordToSplit.IndexOf(" "))
Dim theLastChars as String = theWordToSplit.SubString(theIndex)
end if
theIndex = theIndex + 1
next
The code basically goes through each character, if it finds a space then it gets the first word (everything just before the space which is what the IndexOf does, giving it the index of the space found) and the last words (everything after the space)
this is rough code but hope it gives you some guidelines or some pointers
AdamB78