Parse text vb.net to strings - vb.net

Lets say i have a string which goes like
<image1>C:\images\dropbox\lister\4844-001.jpg</image1><image2>C:\images\dropbox\lister\4844-002.jpg</image2><image3>C:\images\dropbox\lister\4844-003.jpg</image3><image4>C:\images\dropbox\lister\4844-004.jpg</image4>
It contains from Image1 to ImageN
I need to check through each image path and check if the image on that path really exist.
How to loop through that ?
I think that finding the index of word and and pulling all text between is the slowest way.
Can you suggest anything beside that ?
Private Function Word(source As String, start As String, ends As String) As String
Dim sSource As String = source
Dim sDelimStart As String = start
Dim sDelimEnd As String = ends
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd)
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
Return res
Else
Return ""
End If
End Function

In this I converted your string to a XElement, then iterated each of the imageN nodes. This should work so long as your input string is as you specified.
Dim s As String
s = "<image1>C:\images\dropbox\lister\4844-001.jpg</image1><image2>C:\images\dropbox\lister\4844-002.jpg</image2><image3>C:\images\dropbox\lister\4844-003.jpg</image3><image4>C:\images\dropbox\lister\4844-004.jpg</image4>"
s = "<FOO>" & s
s = s & "</FOO>"
Dim fooXE As XElement = XElement.Parse(s)
For Each el As XElement In fooXE.Elements
' Debug.WriteLine(el.Value)
If IO.File.Exists(el.Value) Then
'the path exists
End If
Next

Related

select random from target string VB.NET

how could i make that
Source_Source.Text (richtextbox) :
src="test.com" akalsjdjalksdv src="another.com" asdbnmslkbjcxv
asdas as danms d amsn asdasd src="cold.com"asdas as dasd amnbs dma d sdf kjhds f src="find.com" asd kja sdasjhk d asdsrc="other.com" a jksdh asksjd hasdjh src="found.com"
what if i wanna get random src=" ", for example, if i clicked butten will show message = src="test.com" , another time if i clicked button will show another random, for example src="another.com" ,
my currently code only select first string which = src="test.com" , and i wanna select randoms src="[random test / cold / other / found / find]"
for example my next click can show message for src="find.com" that for example.
my code :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sSource As String = Source_Source.Text 'String that is being searched
Dim sDelimStart As String = Search_1.Text 'First delimiting word
Dim sDelimEnd As String = Search_2.Text 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1) 'Find the first occurrence of f2
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
MessageBox.Show(res) 'Display
Else
MessageBox.Show("One or both of the delimiting words were not found!")
End If
End Sub
Private Function RndUrl() As String
Dim UrlStr() As String = {"whatever.com", "whatever2.com"}
Return Rand.Next(0, UrlStr.Length) '"Rand" Declared at Class scope as Random
End Function
Usage:
TextBox1.Text = "src=""" & RndUrl() & ""
If I understand your question correctly, you're looking for a certain phrase in a String? If so look into String.Contains() method.
For example:
If Source_Source.Text.Contains(Search_1.Text) Then
' Do you're logic here if found
Source_Source.Text.SubString(0, 5) ' Or where ever that part of the string is that you want.
End If

Finding Characters in vb.net string

I have a stack overflow question regarding the following code:
For Each DeltaCharB As Char In DeltaString
If DeltaCharB = "[" Then
Dim DeltaIndexB As Integer = TB8.IndexOf("[B|")
Dim DeltaStringB As String = TB8.Substring(DeltaIndexB + 3, TB8.IndexOf("]", DeltaIndexB + 1) - DeltaIndexB - 3)
MsgBox(DeltaStringB)
Else
'Do nothing
End If
Next
The issue created is that if the code is run X number of times that the character "[" is found, The string is displayed the same X amount of times in a messagebox.
However I only want to have it processed 1X. I have tried to change the following line but as i expected only one character at a time is permitted.
If DeltaCharB = "[B|" Then
Typically the string used to search in would be as follows:
{[A|Text belonging to entry A][B|Text belonging to entry B][C|Text belonging to entry C]}.... ect...ect
Dose anybody know how to resolve this?
Why do you need a loop? Your delimiters are well defined, you can just do this:
Function GetContent(byval input as string, byval delimiter as string) as string
Dim fullDelimiter = "["& delimiter &"|"
Dim BeginPosition as Integer = input.IndexOf(fullDelimiter)
if BeginPosition > -1 then
BeginPosition += fullDelimiter.Length
Dim EndPosition = input.IndexOf("][", BeginPosition)
if EndPosition > -1 then
return input.SubString(BeginPosition, EndPosition - BeginPosition)
end if
end if
return ""
End Function
And the usage:
Dim s as string = "[A|Text belonging to entry A][B|Text belonging to entry B][C|Text belonging to entry C]"
Dim content = GetContent(s, "B")
content now contains "Text belonging to entry B"
Note that with this code, the delimiter can by a string of any length between [ and |.
A more general solution that will fit any input format would mean to also accept the end delimiter in the function:
Function GetContent(byval input as string, byval FromDelimiter as string, byval ToDelimiter as string) as string
Dim BeginPosition as Integer = input.IndexOf(FromDelimiter)
if BeginPosition > -1 then
BeginPosition += FromDelimiter.Length
Dim EndPosition = input.IndexOf(ToDelimiter, BeginPosition)
if EndPosition > -1 then
return input.SubString(BeginPosition, EndPosition - BeginPosition)
end if
end if
return ""
End Function

How can i find a string in a txt file and linenumber

I would like to make a "Tolerance-calculator"
The user gives an input as string. For example:"D6" now i have to search this in the .txt file and read the next line.
I read the file like this:
Dim Findstring = IO.File.ReadAllText("....\Toleranz0.txt")
How can i find the string an the next line after the string?
Maybe:
Findstring.contains("D6") 'gives a Boolean
How does i get the correct line?
Convert your string to an array using String.Split() and find the next index or 2 indexes after "D6":
Private Sub Funcy()
Dim Findstring As String = IO.File.ReadAllText("....\Toleranz0.txt")
Dim MyCollection() As String = Findstring.Split()
Dim result As String = MyCollection(Array.IndexOf(MyCollection, "D6") + 2)
MessageBox.Show(result)
End Sub
Here's an example using ReadAllLines() as suggested by Blorgbeard:
Dim lines() As String = IO.File.ReadAllLines("....\Toleranz0.txt")
Dim index As Integer = Array.IndexOf(lines, "D6")
If index <> -1 AndAlso index <= lines.Count - 2 Then
Dim targetLine As String = lines(index + 1)
' ... do something with "targetLine" ...
Else
' either the line was not found, or there was no line after the found line
End If

English sentence as input and translates it into Textese

working on a project where I input a text file and write a sentence in an "English" text box and with a click of a button the program translates it into "textese" type of language. It will only change words listed in the text file and ignore everything else. Below is the code I have, and wondering what is going wrong, it fails to run and highlights my If Then statement, so I think my issue is there but not sure what. A few examples of what is in the text file and how they are ordered / separated by a comma.
anyone,ne1
are,r
ate,8
band,b&
be,b
before,b4
busy,bz
computer,puter
Public Class frmTextese
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
line = inputData(i)
data = line.Split(","c)
englishArray = line.Split(","c)(0)
texteseArray = line.Split(","c)(1)
If result(i).StartsWith(englishArray(i)) Then
If englishArray(i).Equals(texteseArray(i)) Then
result(i) = texteseArray(i)
End If
End If
txtTextese.Text = result(i)
Next
Loop
End Sub
End Class
You only need to compare result(i) against englishArray. Also, your while loop was an endless loop. When searching the textese array, once you find a match, you can quit searching and go on to the next english word. Finally, you should take care to use variable names that describe their purpose.
Look at this code (untested). I made the string comparison case insensitive, but that is not required.
Private Sub btnTranslate_Click(sender As Object, e As EventArgs) Handles btnTranslate.Click
'Get all the textese definitions
Dim inputData() As String = IO.File.ReadAllLines("Textese.txt")
Dim english As Integer = 0
Dim englishSentence As String = txtEnglish.Text
Dim result() As String = englishSentence.Split(" "c)
Do While english < (result.Length - 1)
Dim line As String
Dim data() As String
Dim englishArray As String
Dim texteseArray As String
For i As Integer = 0 To (inputData.Length - 1)
'Split the textese entry into two parts
line = inputData(i)
data = line.Split(","c)
englishArray = data(0)
texteseArray = data(1)
'Compare the word in the english sentence against the word in the textese array
'using a case insensitive comparison (not required)
If result(i).Equals(englishArray, StringComparison.CurrentCultureIgnoreCase) Then
'Replace the word in the sentence with its textese version
result(i) = texteseArray
'If we found the word, there is no need to continue searching, so
'skip to the next word in the english sentence
Exit For
End If
Next
'Increment the loop counter to avoid an endless loop
english = english + 1
Loop
'Take the elements of the result array and join them together, separated by spaces
txtTextese.Text = String.Join(" ", result)
End Sub

copy fixed content between two delimeter in visual basic

i want to copy or transfer the fixed words between two delimiting symbols from one textbox to another.i was successful in transferring the single word from one text box to another but when i wanted to transfer two or more word,it showed the error.the transferring of the word happens when the button is pressed.
My code for transferring single word between the delimiting symbol is:-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimEnd As String = "." 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
TextBox2.Text = res 'Display
End If
End Sub
Above code works properly but when when i wanted to search and transfer two words it showed the error as ArgumentException was Unhandled and Argument 'Length' must be greater or equal to zero.My error containg code is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sSource1 As String = TextBox1.Text
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimStart1 As String = "LastName="
Dim sDelimEnd As String = "." 'Second delimiting word
Dim sDelimEnd1 As String = "."
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.IndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.IndexOf(sDelimEnd1)
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
TextBox2.Text = res 'Display
End If
If nIndexStart1 > -1 AndAlso nIndexEnd1 > -1 Then '-1 means the word was not found.
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1 + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length) 'Crop the text between
TextBox3.Text = res1 'Display
End If
End Sub
I think the error is due to the repeated use of Length variable in 'if' statement but i don't know how to fix that.The link to the snapshot of my desired output is:http://tinypic.com/r/1zy7iwz/8 i was only able to transfer the word harry but not the porter.Any help is much appreciated.
To start, I think you need to add .length after your sDelimStart and sDelimStart1. Currently, you're trying to pass a String + Integer as an Integer.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.length + 1, nIndexEnd - nIndexStart - sDelimStart.Length)
And in your second if statement:
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1.length + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length)
However, I would recommend using the String.Split function:
https://msdn.microsoft.com/en-us/library/b873y76a%28v=vs.110%29.aspx
EDIT: Why it is displaying same thing in both.
I think this is because you need to find the last index of the delimiters.
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.LastIndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.LastIndexOf(sDelimEnd1)