The question is simple, How do I define a variable which holds double quotes. when I try to define the variable like this
Dim s as String = " " " ,
VS puts an extra quote like this
Dim s as String = """"
The extra " is used to escape the " character, so the sequence of two double-quotes ("") will show up as " when your string is displayed.
You actually have it correct with the 4 quotes, that is VBs way of escaping the quotes. So, for example:
Dim oneDoubleQuote As String = """"
Dim twoDoubleQuotes As String = """"""
MessageBox.Show("One:" & oneDoubleQuote)
MessageBox.Show("Two:" & twoDoubleQuotes)
The first message box has One:" and the second has Two:""
Related
I wish to remove spaces from lines that doesn't contain text, but not remove the line. Since a space character can be hard to identify, I will replace the space character with the "#" (hastag character) to showcase the example easier. The string looks something like this:
"This is
########a long string
with many lines
#######
and the above is empty
####this is empty
#############
#######hello"
I wish that the output would remove the spaces on the lines that only contains the space character. I am still using the "#" (hastag character) to showcase the spaces. The final output should look like this:
"This is
########a long string
with many lines
and the above is empty
####this is empty
#######hello"
Without the hashtag character acting as the space character, the expected output should look like this:
"This is
a long string
with many lines
and the above is empty
this is empty
hello"
So to fully clarify, I wish to remove space characters on a line that doesn't contain text, but not remove the line.
Using your example with octothorpes (yet another name for #) and replacing them with spaces in the code, we can use the String.IsNullOrWhiteSpace function to check for such lines and replace them with empty strings:
Module Module1
Sub Main()
Dim s = "This is
########a long string
with many lines
#######
and the above is empty
####this is empty
#############
#######hello"
s = s.Replace("#", " ")
Dim t = String.Join(vbCrLf, s.Split({vbCrLf}, StringSplitOptions.None).
Select(Function(a) If(String.IsNullOrWhiteSpace(a), "", a)))
Console.WriteLine(t)
Console.ReadLine()
End Sub
End Module
Outputs:
This is
a long string
with many lines
and the above is empty
this is empty
hello
Use the following code:
Dim container As String = IO.File.ReadAllText("test.txt")
container = container.Replace(vbNewLine, vbCr).Replace(vbLf, vbCr)
Do While container.Contains(vbCr & vbCr)
container = container.Replace(vbCr & vbCr, vbCr)
Loop
If container.StartsWith(vbCr) Then container = container.TrimStart(Chr(13))
container = container.Replace(vbCr, vbNewLine)
IO.File.WriteAllText("test.txt", container)
It'll trim all the empty new lines and override the file (before vs. after):
Note: If you want to remove the hashes and replace that with white spaces too, just use the following:
Dim container2 As String =
My.Computer.FileSystem.ReadAllText("test.txt").Replace("#", " ")
IO.File.WriteAllText("test.txt", container2)
Try this
Dim myString = "your multiline string here"
Dim finalString As String
For each line In myString.Split(CChar(vbNewLine))
finalstring &= line.Trim() & vbNewLine
Next
I have two comma separated string. this String actually connected with database.
This the example of string what I actually want to use.
e.g.
Dim value As String = "One,Two,Three"
Dim mtr As String = ",,,"
I use the following code
Dim elements() As String = value.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Q_MTR() As String = mtr.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To elements.Length - 1
MsgBox(elements(i) & " Mtr="& Q_MTR(i))
Next
I want to show the Output Like:
"One Mtr= "
"Two Mtr= "
"Three Mtr= "
But I got Error
"Index was outside the bounds of the array."
Can anyone please tell me how can I solve this prblm? in VB.net.
I think using Trim with StringSplitOptions.RemoveEmptyEntries doesn't work because " " isn't considered an empty entry. I need to do a normal split, then trim each item, then filter out the empty strings. So I change StringSplitOptions.RemoveEmptyEntries to StringSplitOptions.None.
I use this code and it's work :)
Dim Q_MTR() As String = mtr.Split(New Char() {","c}, StringSplitOptions.None)
I have string that is formatted in UTF-8.
"{""messages"":[{""messageId"":""245043"",""campaignId"":""14085""
I need to replace the double double quotes to single double quotes.
The following is able to replace double quotes to double single quotes
NewMessage = Replace(Message, "““", "''")
But I can't figure out how to replace the double double quotes to single double quotes.
The code was written in vb.net and the desired output is:
"{"messages":[{"messageId":"245043","campaignId":"14085"
Added image from watch
Solution Provided by o_O
You can leverage the Chr function for this:
NewMessage = Message.Replace(Chr(34) & Chr(34), Chr(34))
I assume your data is coming from somewhere in
"{""messages"":[{""messageId"":""245043"",""campaignId"":""14085"" format, so for the sake of the question I'm formatting the input in a proper way:
Dim str As String = """{""""messages"""":[{""""messageId"""":""""245043"""",""""campaignId"""":""""14085"""""
Console.WriteLine("Input : " & str)
str = str.Replace("""","'").Replace("''","""").Replace("'","""")
Console.WriteLine("Output : " & str)
Output:
Input : "{""messages"":[{""messageId"":""245043"",""campaignId"":""14085""
Output : "{"messages":[{"messageId":"245043","campaignId":"14085"
Check it on Fiddle
I have inserted a option in Dorpdown as follows
<option>إختر </option>
When I select this text from server side on any event I get this value
"إختر "
Now I want to replace this white space in the string. I have tried replace method of String class. But its not working.
str = str.replace(" ","")
Plz suggest
What you should do first is decode the HTML, such that text like but also & are converted to their textual counterparts (" " and "&"). You can do this with: WebUtility.HtmlDecode. Next you can use String.Trim to remove leading and tailing spaces.
Example:
string s = "إختر ";
string r = WebUtility.HtmlDecode(s).Trim();
Or the VB.NET equivalent:
Dim s As String = "إختر "
Dim r As String = WebUtility.HtmlDecode(s).Trim()
Evidently you can try to convert to spaces yourself. But there are examples where it is not that evident and your transcoder can get confused or decode strings the wrong way. Furthermore if in the future the people at W3C change their minds about how to encode text in HTML/XML, then your program will still work.
String.Trim will remove all kinds of white-space including spaces, new lines, tabs, carriage returns, etc. If you only want to remove spaces, you can use: .Trim(' '). Then you specify only to remove the given list of characters (here only ' ').
If you want to remove leading or trailing white-spaces from a string you just need to use String.Trim, but you have to re-assign the return value to the variable since strings are immutable:
string text = "إختر ";
text = text.Trim();
Note that you can also use TrimEnd in this case.
If you want to remove only space characters(not also tabs or new-line characters which are also white-spaces) use:
text = text.Trim(' ');
If you instead want to remove all spaces from a string you could do:
text = text.Replace(" ", "");
I think maybe your code is something like this
Dim str As String = "إختر "
str.Replace(" ", "")
But actually you should
Dim str As String = "إختر "
str = str.Replace(" ", "")
I have just had a similar problem.
It turns out, that this nbsp character is Chr(160) from the ASCII table. Thus, something like this is quite meaningful, for all the cases. It works, on a selected area:
Public Sub remove_space_in_string()
Dim r_range As Range
For Each r_range In Selection
r_range = Trim(r_range)
r_range = Replace(r_range, vbTab, "")
r_range = Replace(r_range, " ", "")
r_range = Replace(r_range, Chr(160), "")
Next r_range
End Sub
Can someone please tell me a way (or similar) to use (") in a text in vb.
eg. msgbox("some text here "other text here" more text" & vbnewline & "next line of text").
If you still dont get what I mean, all I need is this section ("other text") including the (") to show in a msgbox.
Either escape the quote by another quote Dim q as String = """" would be one quote, or use ControlChars.Quote
All you have to do is use double quotes.
For example : Dim abc as string = "Using double ""quotes""
So for MsgBox you would do the same. MsgBox("Use double ""quotes"" to show quotes")
Escaping the quotes never looks quite right to me.
I like using a fluent-ish API instead:
<Extension>
Public Function Quote(byval value as String) As String
Return value & CHR(34)
End Function
<Extension>
Public Function SurroundWith(ByVal value As String, ByVal surround As String) As String
Return surround & value & surround
End Function
Usage:
Console.WriteLine("Hello there! I'm Bob ".Quote & "Buddy".Quote & " Holly.")
Console.WriteLine("Hello there! I'm Bob " & "Buddy".SurroundWith(CHR(34)) & " Holly.")