Remove "</div>" (and anything after it) from a string - vb.net

I would like to remove the substring "</div>" from a larger string. Since this does not necessarily appear at the end, any text appearing after this token should be removed as well. Since Split and Remove only allow integers, how would I do this?
For example, after making the changes
"Wanted text</div> arbitrary text" becomes "Wanted text"

Although your question appears to be "incomplete", reading through the comments it appears that you want to remove the closing DIV tag along with whatever text appears after it.
If that's the case, then this code should do the work:
Dim txt As String = "Wanted text</div> arbitrary text"
Dim p As Integer = txt.ToLower().IndexOf("</div>")
If p <> -1 Then txt = txt.Substring(0, p)

You can do this with .Split() without needing to check for the existence of <div> or getting the location.
Dim txt As String = "Wanted text</div> arbitrary text"
txt = txt.Split( New String() {"</div>"}, StringSplitOptions.None )(0)

Related

How to search only the first line of a multiline textbox in VB.NET

Is there any way to search only the first line of a Multiline Textbox without knowing exactly at what position the text is you're looking for?
If I knew the position of the text I was looking for I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Substring(0,4) = myText Then
' Do Something
End If
Or if I wanted to search the entire textbox I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Contains(myText) Then
' Do Something
End If
But I want to search only the first line of the textbox and I'm not sure at what position the text may be. Is there anyway to do a search like that?
This is another example of why you should ALWAYS read the relevant documentation. If you had read the documentation for the TextBox class then you'd know that it has a Lines property. To get the first line of text, you simply get the first element of that array:
Dim firstLine = myTextBox.Lines(0)
If Not filrstLine.Contains(myText) Then
'Do something
End If
Note that this only applies where the user has explicitly added a line break to the text. I assume that that is what you want, given that you have accepted another answer that does the same thing. If you mean the first line based on automatic word-wrap then that requires a bit more effort.
You could take the text and extract the first line.
int pos = text.IndexOfAny('\r', '\n');
if (pos >= 0)
text = text.SubString(0, pos);
// text now contains only the first line
Then you can search the resulting string.

How to get this value?

Ive been trying to get this value from wb forever now.. And can't do it.
What I am trying to get is "388399"
However "ember557" changes constantly to "ember'random number'" and the class is not unique.
<div id="ember557" class="ember-view">388399</div>
Maybe you could try to get it with the CLASS instead of the ID
you can call GetElementByTagName and then iterate through this to find the element of the relevant class
And if that doesnt work then you could also try to use regex to get the string you want. Then replace the unwanted characters.
Dim myString As String = "your html string here"
Dim regex = New Regex("<div id=""ember[0-9]*"" class=""ember-view"">[0-9]*</div>")
Dim match = regex.Match(myString)
If match.Success Then
'replace the unwanted characters.
End If

Grab text from webpage using vb

i want code that grab text from webpage
here is the html
<div><span>Version : </span> " 1.3"</div>
so i want 1.3 text in textbox1
To manipulate HTML elements/documents easily, you need to install HTML Agility Pack. You can get it from NuGet at: https://www.nuget.org/packages/HtmlAgilityPack
After you have it, you can do a lot of magic with HTML documents/tags.
Dim voHAP As New HtmlAgilityPack.HtmlDocument
voHAP.LoadHtml("<div><span>Version : </span> "" 1.3""</div>")
Dim voDiv As HtmlAgilityPack.HtmlNode = voHAP.DocumentNode.Elements("div")(0)
voDiv.RemoveChild(voDiv.Element("span"))
Dim vsText As String = Replace(voDiv.InnerText, """", "").Trim
The vsText variable will contain your value of 1.3. The final Replace() function is to remove the unwanted " characters in the string.

VB.Net Writing to Txt File

I'm trying to write the content of my textbox to a txt file.
My code works fine but my error is, when I open txt file I see
writeline1writeline2writeline3
instead of
writeline1
writeline2
writeline3
my code;
result As List(Of String) = New List(Of String)
convertedText.Lines = result.ToArray()
My.Computer.FileSystem.WriteAllText(mypath & "\convertedcontent.txt", convertedText.Text, False)
Writing to .csv and many other file types work fine but I don't know how to break lines for text file.
Thanks in advance
I would use System.IO.File.WriteAllLines:
Dim path = System.IO.Path.Combine(mypath, "convertedcontent.txt")
System.IO.File.WriteAllLines(path, result)
Otherwise you need to append Environment.NewLine to each line, you can use String.Join:
System.IO.File.WriteAllText(path, String.Join(Environment.NewLine, result))
You need to add & vbCrLf to your strings (each line)
Not sure where you are getting your strings from.. but you will have to add the carrier return/Line Feed character to those strings, one at the end of every string.
Might just even loop through your array and add them there?
P.S. Some of the comments have quicker ways of getting there, but this is probably what happens behind the scenes...
for i = 0 to convertedText.Lines.count -1
convertedText.Lines(i) += vbCrLf
next

link is getting truncated, any ideas why?

I have been making some progress on this but still have some issues to resolve.
Hopefully, this one won't be that hard.
I have this:
For Each item In Request.QueryString("doc").Split(","c)
sb.Append("http://default.html?k=")
sb.Append(item)
sb.Append("&p=2&o=m</p>")
Next
When I test this code:
Response.Write(sb.ToString())
I get:
http://default.html?k=122&p=2&o=m
http://default.html?k=123&p=2&o=m
That's exactly what we are looking for
When we assign it to a variable like:
Dim linkList As String = sb.ToString()
However, when I loop through linkList
and write it to the screen, it is spitting out only the letter h.
Any ideas what I am doing wrong and how to fix it if possible?
Dim link As String
For Each link I linkList
'let me know if I am still getting the links
response.write link
'we will save all the links later
Next
As always, thanks a lot for your help
You can not loop through a string and get another string (note: linkList is a String - Dim linkList As String = sb.ToString()). That's why you get the h it's trying to pick the Chars in the string. Place the strings in an array then loop through the array. Try this:
Dim linkArray() As String
For Each item In Request.QueryString("doc").Split(","c)
Dim stb As New StringBuilder
stb.Append("http://default.html?k=")
stb.Append(item)
stb.Append("&p=2&o=m</p>")
linkArray.add(stb.toString())
Next
For Each link As String In linkArray
response.write link
Next
It looks like you are not closing your <a> tag
&p=2&o=m</p>
Notice the </p> to close a paragraph without the closing <a> tag with </a>.
Try to view source and you'll probably see the HTML is malformed in that way.