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"
Related
I'm trying to remove the last part of the string to make it more presentable. The string looks like this
Stringname.number.RemainingStringTobeRemoved
Is there a way to remove the last part without using string.Substring(0,string.Length-10)?
I want it to be dynamic, the string I'll be removing is not a constant number.
My initial idea was to use delimiter to identify the starting point of the part I want to remove.
My initial idea was using delimiter to identify the starting point of the part I want to remove.
That's a good idea if your string is always the same format and the substring you want to remove does not contain that delimiter. Take a look at LastIndexOf.
Spoiler:
Dim s = "Stringname.number.RemainingStringTobeRemoved"
Dim r = s.Substring(0, s.LastIndexOf("."))
You can use Split to achieve this
String s = "Stringname.number.RemainingStringTobeRemoved"
Dim words As String() = s.Split(New Char() {"."c})
String RequiredString = words(0) & words(1)
The RequiredString is what you need
Using a delimiter is one way. You can use the Split method to split the string into parts at the ..
Dim FullString As String = "Stringname.number.RemainingStringTobeRemoved"
Dim StringParts() As String = Strings.Split(FullString, ".", 3)
Return StringParts(0) & StringParts(1)
The Split method returns an array of String that contains the parts. The array will contain the following items
Stringname
number
RemainingStringTobeRemoved
The delimiters are omitted.
I'm trying to remove various words from a text field in MS Access. The data might look like:
Hi there #foo, what's new #bar
#goodfriend and I just watched Star Wars
#this and #that and #theother
I want to remove all the words that start with '#'.
Replace() won't work since the words are different in each record.
Any ideas?
If you're using VBA, you should be able to replace text based on regular expressions. See the answer to replace a column using regex in ms access 2010 as an example.
I actualy upvoted CheeseInPosition's answer but just thought I would provide another way if you can't/don't want to do it with regex. Just wrote a quick function to parse out the words:
Public Function RemoveAtWords(strOriginal As String) As String
Dim strParts() As String
Dim i As Integer
strParts() = Split(strOriginal, " ")
For i = LBound(strParts) To UBound(strParts)
If Left(strParts(i), 1) <> "#" Then
RemoveAtWords = RemoveAtWords & " " & strParts(i)
End If
Next
RemoveAtWords = Trim(RemoveAtWords)
End Function
You can call that from a query and pass through your string. Not as efficient because you have to loop through the whole string, but just another option.
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"
Hi I want to extract specific string from label's text. How to achieve it. I thought of one way using regex, now I know how to match to a regex but don't know how to extract.
e.g. The label text is
name: tom
I want to extract
tom
i.e.
(:)([a-z]*)(\n)
How can be this achieved using visual basic 2010?
You can use the remove method of string objects. This code would produce a messagebox that just says "Tom".
Dim mystring As String = "name: Tom"
MessageBox.Show(mystring.Remove(0, 6))
Assuming you just want text after the first colon character, a simple Substring would work where you find the index of the first colon character:
string test = "Name: Tom";
string result = test.Substring(test.IndexOf(":") + 1).Trim();
If you just want tom, you can try to do this:
dim t as string = "tom"
replace(tom, "name: ", "")
then your variable t will hold "tom"
Trying to split a line wherever "," appears (with the quotation marks). The problem is VB.NET uses " to start/end strings, so I tried using .Split(""",""") but that then splits it by " not ",".
Try something like this:
Dim TestToSplit As String = "Foo"",""Bar"
Dim Splitted() As String = TestToSplit.Split(New String() {""","""}, StringSplitOptions.None)
I just tested it and got an array with Foo And Bar. I hope this helps.
The Split function (the way you are using it) expects a Char. If you want to split on multiple characters you need to use a string array. (Seems to me another overload of a single string value would have been handy.)
This function splits a line of text and returns an array based on the delimiter you have specified. (Of course, you could make this more general purpose by passing in the separator array.)
Private Function SplitLine(ByVal lineOfText As String) As String()
Dim separator() As String = {""","""}
Dim result() As String
result = lineOfText.Split(separator, StringSplitOptions.None)
Return result
End Function
Another alternative I often find useful is this:
Regex.Split(textToSplit, """,""")
Lets you split on more complex criteria than an array of alternative separators.
To escape the "-character in VB.NET, use two: ""