I have a text file with data pieced together needs to be separated onto separate lines. Not sure how to go about doing it, still new to vb.
I'm thinking about something along the lines of:
If sLine.EndsWith(", ") Then
"Do Something"
End If
So for anyone wondering if they come across this post, I'm new to vb.net so I'm still discovering all sorts of tricks to the language. One such "trick" resolved this issue, and that is that you can use the carriage return from VB6 (vbCr). This separated all of the criteria I needed set to the next line, thus, resolving the issue for me.
So this line of code:
subchild = subchild.Replace(", '", vbCr)
Would turn this:
ABCDEFG, HIJKLMN, OPQRSTU
Into this:
ABCDEFG
HIJKLMN
OPQRSTU
Related
I'm pretty much a newbie to VBA and Access and I start to get kinda frustrated.
I'm working on a program for calculating prizes and therefor I got HTML-Datas with the necessary informations which get split up as following:
strHTML = GetHTML(HTML)
List() = Split(strHTML, Chr(10))
Just to make it clear, the HTML lines I pick up for this look as following:
Motortyp=SM132
Motortypreihe=SM
MotorBG=132
MotorFE=35
MotorZusatz1=B
MotorZusatz2=
ReglerTyp=Fremdregler
So far so good.
The whole gear description (e.g. "SM200.15C") is taken at once and somehow there still is kind of a line break afterwards which leads to me not being able to use it in queries as I want to.
It seems like the line break at which I split up the HTML somehow is still present and it just don't want to get replaced by stuff like Replace([Data], Chr(10). "").
Hope the question was Kind of understandable since I got a few brain lags today.
Thanks in advance,
Dennis
On Windows a newline is obtained from the combination of a carriage return Chr(13) and linefeed Chr(10) characters. Use
List() = Split(strHTML, vbCrLf)
where vbCrLf is a VB constant representing this pair of characters.
TL;DR:
Is there a way to embed a console inside a form, so it becomes a part of it?
Scenario:
I am writing a chat-application with client, server and database using Windows Forms in VB.NET. The server should log all communication it has with clients in a textbox.
The Problem:
So far that would not be a problem - If there wasn't a maxlength for strings! I expect this server to almost never stop (Okay, there is always some point where it does.. but lets ignore that). So if i go for Textbox1.Text &= vbnewline & "Something" it will some day reach this length and run into an exception each time something is about to be logged. I also don't want to just remove the first characters of the string.
My Idea for a solution:
My idea for a work around: Use a console instead of a simple textbox and embed it into the form, so it becomes a part of it. Is there a simple way to do that? By simple I mean that I should have to write thousands of lines of code to achieve that goal.
I am open minded for different ideas and ways to do so as well.
Why not just log your chat to file (you could create a file per day)?
Dim filename = String.Format("C:\Temp\{0:yyyy-MM-dd}-log.txt", DateTime.Now)
My.Computer.FileSystem.WriteAllText(filename, "line of text", True)
Then to display it - use a ListBox and append each new line to the end of the listbox? Each time you append, you could check how many items are in the listbox and remove the first 100 if you are over 1000 as an example.
I have a csv file, when i use the split function, my issue is that the 16th segment of the array has a name in it (in most cases) that has first and last name split by a comma. This obviously causes me issues as it puts my array out of sync. any suggestions on how i can handle this?
the string in the 16th segment is surrounded by "" if that helps, the split function still splits it though.
you can use TextFieldParser as indicated here
I recommend Lumen CSV Library, it can correctly handle field values with commas.
Also it has a very good performance, and a very simple usage.
See the link above, it won't disappoint you.
I think you're missing the point. Split is only good for simple csv parsing. Anything that gets even a little complicated means a lot of extra code. Something like the TextFieldParser is better suited to what you want. However if you must use Split here's one way:
Dim TempArray() As String
Dim Output As New List(Of String)
If SourceString.Contains("""") Then
TempArray = SourceString.Split(""""c)
Output.AddRange(TempArray(0).Split(","c))
Output.Add(TempArray(1))
'If the quoted part of the csv line is at the end of the line omit this statement.
Output.AddRange(TempArray(2).Split(","c))
Else
Output = New List(Of String)(SourceString.Split(","c))
End If
This assumes that the data is strictly organized, except for the quotes, if not you'll have to add validation code.
Split by "," with the quotes instead of just a comma. Don't forget to take care of the first and last quotes on the line.
I am writing a script in VBscript and I need to include line breaks or carriage returns. I was using the following code:
objSelection.TypeParagraph()
But this creates to big of a space between lines. The best way I can think to explain it is, when you are in Microsoft word, and you type a line, if you were to press Shift + Enter, you would get two separate lines close together, whereas pressing just Enter will do a full Carriage return (which I don't want). I will demonstrate here.
How can I achieve the same result as 'Shift + Enter' but through VBscript?
Thank you very much.
Assuming you're asking about Word VBA, you can do it like this:
objSelection.TypeText(Chr(11))
Generally, if you can't find out how to do something in VBA that you can do in the application, just record a macro while you perform the required steps and then look and see what VBA has been generated.
EDIT: Accepted answer points out what my issue was. I added another answer which shows an even easier way.
I have a multiline textbox from which I create a single string:
Dim wholeThing As String
Dim line as String
For Each line In txtMultiline.Lines
wholeThing = wholeThing & line & Environment.Newline
Next
What I'd like to do is write this to a DB as-is by adding wholeThing as a parameter using to SqlCommand.Parameters.AddWithValue, but all of my painstakingly-inserted newlines go away (or appear to).
I saw this and was hoping I didn't need to concern myself with CHAR(nnn) SQL stuff.
Any suggestions? Thanks!
What do you mean when you say "appears to"? A newline character can be easily stored in an nvarchar field. What tools are you using to verify the results of your actions?
It's even easier than this. As per the discussion in the accepted answer, I wasn't seeing all of the lines in my view. Putting up a messagebox showed everything.
What's more, I don't need to take things line by line:
wholeThing = txtMultiline.Text
works, too, and it keeps all of the line breaks.