I have a string that is 109000 characters in length, and it wont fit on one line, I get a "line is too long." error.
I know that with normal CODE you can do the "_" at the end of your code like
this code is too long so I will use the _
this code acts like it is on the same line
but in a string it takes the "_" to be part of a string (as it should). There is no information that I could find on this, so here it is for you guys, stackoverflow.
The documentation on the "Line is too long" error states that the maxiumum line length is
65535, so this is why you are getting the error.
There are a few solutions:
You can concatenate the string using the &
Dim s As String = "this code is too long so I will use the" &
"this code acts like it is on the same line"
Note you can also use + to concatenate string but make sure you have Option Strict On if you do (& is safer as the result is always a String). See comparison between the two here: Ampersand vs plus for concatenating strings in VB.NET
You can use a string builder. This may be more efficient if you are continually adding strings to the original string (especially if you do this in a loop):
Dim sb As new StringBuilder
sb.Append("this code is too long so I will use the")
sb.Append("this code acts like it is on the same line")
Debug.Writeline(sb.ToString)
See MSDN for More information about Concatenation here
Dim longString As String = "loooooooooooooooooooooooooo" + _
"ooooooooooooooggggggg"
Related
I'm new to programming, and I have one small question. I wrote a code to create a xml file with 5 lines and everything works like a charm. Now I have a textbox and I want the input from the box, named INPUT be written in the middle of one line. This is my code:
Private Sub Entry_Click(sender As Object, e As EventArgs) Handles Entry.Click
Dim data As String() = {
"<?xml version=""1.0"" encoding=""utf-8""?>",
"<EntryQue xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">",
"<NumberReq>0</NumberReq>",
"<TypeReq>7</TypeReq>",
"<Amount>", INPUT.Text, "</Amount>",
}
File.WriteAllLines("C:/Prog/xml/DATA.xml", data)
The problem is that the INPUT is written in a new line, between amount and /amount, like this:
Amount
123456
/Amount
How can I put it in the same line...for example Amount123456/Amount ?
Your array of string is delimited by commas, so each comma indicates a new line in the output file. Instead of this:
"<Amount>", INPUT.Text, "</Amount>"
Do this:
"<Amount>" & INPUT.Text & "</Amount>"
For details on the string concatenation operators (there are two), see here. I use the & operator because you are concatenating mixed data type (numeric & string):
https://msdn.microsoft.com/en-us/library/te2585xw.aspx
The + Operator (Visual Basic) has the primary purpose of adding two numbers. However, it can also concatenate numeric operands with string operands. The + operator has a complex set of rules that determine whether to add, concatenate, signal a compiler error, or throw a run-time InvalidCastException exception
The & Operator (Visual Basic) is defined only for String operands, and
it always widens its operands to String, regardless of the setting of
Option Strict. The & operator is recommended for string concatenation
because it is defined exclusively for strings and reduces your chances
of generating an unintended conversion.
I am writing a program in Visual Basic that writes and reads to and from a Microsoft Access Database. When reading from the database, one of the functions that I am trying to perform is to determine the number of lines in a multi-line string that was written to the database and then subsequently read from the database. Here's what I have tried so far with no luck.
Dim stringLines() As String = databaseReader("multilineString").ToString.Split(CChar("Environment.NewLine"))
Dim stringLinesCount As Integer = stringLines.Length
For some reason, this always results in stringLinesCount being equal to one, regardless of how many lines the string has. In this example, I am using Environment.NewLine, but I have tried \n, \r, vbCr, vbLf, and vbCrLf as well, and they all result in a value of one. Since none of these seem to be working, what can I use instead to determine the number of lines?
Edit:
Dim splitCharacters() As Char = {CChar(vbCrLf), CChar(vbCr), CChar(vbLf), CChar(Environment.NewLine), CChar("\n"), CChar("\r")}
Dim stringLines() As String = databaseReader("multilineString").ToString.Split(splitCharacters)
Dim stringLinesCount As Integer = stringLines.Length
Since Chris Dunaway provided the answer that I view as helpful but posted it as a comment, here's what he said:
VB cannot use C# style escape sequences, so CChar("\n") and CChar("\r") is meaningless in VB. Also, calling CChar("Environment.NewLine") is wrong because you are trying to convert the actual string "Environment.NewLine" to a single character, which obviously won't work. You can just use Environment.Newline directly in the call to String.Split.
If Chris decides to post his comment as an answer, please let me know so that I may remove this.
I'm trying to write an If then statement to see if a string has a space in it. If it does, I want it to put an " and " around the variable. Below is my current code:
If ColumnText.Contains(" ") Then
MsgBox(""" & ColumnText & """)
End If
Next
But it's seeing quoting everything... Any suggestions?
To properly escape a double quote inside VB's double quoted string literal, you need to double it (no pun intended). This means an empty string "". When you squeeze a quote in it, you get 4 quotes """", and this really means just one double quote literal.
You should be using:
MsgBox("""" & ColumnText & """")
Instead of:
MsgBox(""" & ColumnText & """)
Another thing - notice how the syntax parser highlights your line when it has 3 quotes. In this case & ColumnText & is part of the literal, instead of being an inline variable.
Reference:
String literals # MSDN.
Using quote literals makes for hard to read code and as seen here, can easily lead to errors. I find it much easier to use (and read) String.Format and isolate things you want to call out differently. For instance:
msg = String.Format("There is a problem with [{0}]", columnText)
The result: There is a problem with [foobar]
If you really like quotes, or need for something else like a command line argument, you can still make the code more legible this way:
Const quote As String = """"
' or
Private quote = Convert.ToChar(34) ' 34 is the code for the quote char
'...
msg = String.Format("There is a problem with {0}{1}{0}", quote, columnText)
The result: There is a problem with "foobar" In cases where there are multiple things to wrap with quotes, you just repeat {0} for each as shown.
You can try this:
If ColumnText.Contains(" ") Then
MsgBox(Chr(34) & ColumnText & Chr(34))
End If
Next
To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))
this might be a super easy question but it is not. I am wondering how to put only a single " in a string
Example
Dim eg as String
eg = ""W"
It just does not seem to work. Is there any special characters for it as i need to add in codebehind for my VB.
If it's Visual Basic you need one more ":
Dim eg as String
eg = """W"
If it's C#, use the \ escape character: "\"W"
http://msdn.microsoft.com/en-us/library/267k4fw5.aspx
Just double it:
Dim eg as String
eg = """W"