How to insert " character in string variable? - vb.net

How to insert this?
Dim SpCharacter As String = " " "
or
Dim XMLSitemap As String = "<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">"
insert with all Content " and > and etc

You need to escape it by doubling it:
Dim SpCharacter As String = " "" "
Dim XMLSitemap As String = "<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"">"
See String Basics in Visual Basic on MSDN:
Visual Basic interprets two quotation marks in a string literal as one quotation mark in the string.

In VB.NET you can escape " by passing it twice in a row:
Dim SpCharacter As String = " "" "
or
Dim XMLSitemap As String = "<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"">"

Related

VB.net Html convertor error giving no change?

net language.
and I would like to replace the html first tags and keep the structure of the text, I have tried this code below from the website https://beansoftware.com/ASP.NET-Tutorials/Convert-HTML-To-Plain-Text.aspx
Dim html As String = "<div class='WordSection1'><p class='MsoNormal'>"
Dim final_result As String
Dim sbhtml As StringBuilder = New StringBuilder(html)
Dim OldWords() As String = {" ", "&", """, "<", ">", "®", "©", "•", "™"}
Dim NewWords() As String = {" ", "&", """", "<", ">", "®", "©", "•", "™"}
For i As Integer = 0 To i < OldWords.Length
sbhtml.Replace(OldWords(i), NewWords(i))
Next i
Console.WriteLine($"result after loop : {sbhtml}")
sbhtml.Replace("<br>", "\n<br>")
sbhtml.Replace("<br ", "\n<br ")
sbhtml.Replace("<p ", "\n<p ")
final_result = Regex.Replace(sbhtml.ToString(), "<[^>]*>", "")
Console.WriteLine(final_result)
However the output come back as the same as the string
The for-statement is wrong. It should be
For i As Integer = 0 To OldWords.Length - 1
Probably some C# syntax leaked over.
Why didn't you append the sbhtml.Replace("<br>", "\n<br>") and following lines to the OldWords and NewWords? They are technically not any different.
By using tuples, you can put the old and new words into the same array and use a For-Each-loop
I suggest the following approach
Dim html As String =
"<div class='WordSection1'>aaa<br>bbb<p class='MsoNormal'>"
Dim final_result As String
Dim sbhtml As StringBuilder = New StringBuilder(html)
Dim Substitutions() As (old As String, repl As String) = {
(" ", " "), ("&", "&"), (""", """"), ("<", "<"),
(">", ">"), ("®", "®"), ("©", "©"), ("•", "•"),
("™", "â„¢"), ("<br>", "\n<br>"), ("<br ", "\n<br "), ("<p ", "\n<p ")}
For Each subst In Substitutions
sbhtml.Replace(subst.old, subst.repl)
Next
Console.WriteLine($"result after loop : {sbhtml}")
final_result = Regex.Replace(sbhtml.ToString(), "<[^>]*>", "")
Console.WriteLine(final_result)
The HtmlAgilityPack does a very good job in HTML manipulation and is very reliable. You would do something like
Dim plainText As String = HtmlUtilities.ConvertToPlainText(html)
See Install and manage packages in Visual Studio using the NuGet Package Manager for the easy installation of the HtmlAgilityPack.

Split text in all lines inside of a string

Originally the Keywords will we pulled out from a text file located on a web server.
Dim KeyWords As String = Split(Split(TempSMTPFile, "# Keywords #")(1), "# Keywords #")(0)
I want to create a function that will split out all the keywords inside a string
The keywords in this example will be:
' This list might be changed to more or less keywords.
Dim KeyWords As String = "[One=Test1]" & vbNewLine & "[Two=Test2]" & vbNewLine & "[Three=Test3]"
' I Need a function to split out all keywords and do below check between all splits.
If KeyWords.ToLower.Contains("[" & Splited_keyword & "=") Then ' One, Two, Three
MsgBox(Split(Split(KeyWords, "[" & Splited_keyword & "=")(1), "]")(0)) ' Test1, Test2, Test3
End If
' This should print OneTest1, TwoTest2, ThreeTest3
Thanks for your comments, I was able to solve this with this solution.
Dim str As String() = keywords.Split(New [Char]() {CChar(vbCrLf)})
For Each s As String In str
If s.Contains("[") Then
Dim SplittedKeyword = Split(Split(s, "[")(1), "=")(0)
If TextUserShortDescription.Text.ToLower.Contains(SplittedKeyword.ToLower) Then
MsgBox(SplittedKeyword & (Split(Split(s, "[" & SplittedKeyword & "=")(1), "]")(0)))
End If
End If
Next
This could help you (untested):
Dim keyValues as String()
keyValues = KeyWords.Split(Environment.NewLine)
For Each (keyvalue as string in keyValues)
MsgBox(keyvalue.SubString(1, keyvalue.IndexOf("["c) + " " + keyvalue.SubString(keyvalue.IndexOf("="c), keyvalue.LastIndexOf("]"c)
End For

Insert a string at specified indexof position using VB.NET

I'm trying to insert the string " " or a blank space in a specified index on a textbox like so:
textbox = heybrowhatsup
I want to insert an " " on the indexes 4, 8 and 14, to get "hey bro whats up", but my code just won't work.
My code:
Dim str As String = sum2.Text
Dim insStr As String = " "
Dim strRes As String = str.Insert(15, insStr)
Dim str As String = sum2.Text
Dim insStr As String = " "
Dim strRes As String = str.Insert(3, insStr)
strRes = strRes.Insert(7, insStr)
strRes = strRes.Insert(12, insStr)
You must use strRes.Insert for second or more.
Any string manipulation creates a new String object. What you're doing is working perfectly in that it is creating a new String with the specified substring inserted at the specified position. As is always the case, if you want that String displayed in your TextBox then you must assign that String to the Text property of that TextBox.
The Space function is useful for formatting output and clearing data in fixed-length strings.
You can use space function as below.
Dim str As String = "heybrowhatsup"
Dim strRes As String = str.Insert(3, Space(1)).Insert(7, Space(1)).Insert(13, Space(1))
Or
You can do the same with insert function.
Dim str As String = "heybrowhatsup"
Dim insStr As String = " "
Dim strRes As String = str.Insert(3, insStr).Insert(7, insStr).Insert(13, insStr)

How to keep character " as a part of string

I am new to VB
Dim myStr As String
myStr = "00101"
Dim Rat As String
Rat = "0"
I wish to build new string that will contain myStr but with character "
I mean that final string FinStr should look like this:
FinStr = "AT+COPS=1,2,"00102",0" //where 0 is RAT
So how to keep the character " ?
What is the best function to build string?
Can I do:
FinStr = "AT+COPS=1,2,\"" & myStr & "\"," & Rat
Thanks
There are also a set of Control Characters available:
Try
Dim myStr As String = ControlChars.Quote & "00101" & ControlChars.Quote
I find this much more readable than multiple quotes stacked together.
If I understand your expected result, I would think something like this is what you want:
Dim myStr As String
myStr = ControlChars.Quote & "00101" & ControlChars.Quote
Dim Rat As String
Rat = "0"
FinStr = "AT+COPS=1,2," & myStr & "," & Rat
There are two ways to put double quotes in your string. The first option is to double the amount of quotes:
Dim MyString As String = """Hello, I am a string!"""
Notice that there are three quotes in the beginning and end of the above string. This is because the very first and the very last one represents the beginning and end of a string.
In order to put a quote in the middle of the string you only have to type two quotes "". Example: "He stopped for a moment, and yelled: ""I like bananas!"" "
The second option is to use the Chr() function:
Dim MyString As String = Chr(34) & "Hello, I am a string!" & Chr(34)
Chr(34) represents the quotation mark character.
If you want to read more about the Chr() function: https://msdn.microsoft.com/en-us/library/613dxh46%28v=vs.90%29.aspx
And if you want to read more about putting quotation marks in your string: https://msdn.microsoft.com/en-us/library/267k4fw5%28v=vs.90%29.aspx

How can i replace " in vb.net

How can i replace this string:
<say"Hello">
with another strings in VB.net?
this program is not working!!
Text1.Text = Text1.Text.Replace("<say"Hello">", " ")
To get a literal quote in a string, use a double quote ("") as so:
Text1.Text = Text1.Text.Replace("<say""Hello"">", " ")
The string will then be interpreted as this:
<say"Hello">