select random from target string VB.NET - 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

Related

Parse text vb.net to strings

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

How can I insert a string into other certain strings that meet a criteria?

I'm currently designing a program that takes user input, converts that (if in English) to Pig Latin and vice versa. When you click on the menu button or press F3, the conversion happens. This is what the specific block of code is currently:
For i As Integer = 0 To words.Length - 1
If inputTextBox.Text Like "" Then
'i = words(i).Insert(i, "--way")
outputTextBox.Text = words(i)
End If
Next
What I want to do is, if text inside meets criteria such as if input inside the input text box starts with a, e, i, o or u, that word should get a "--way" tagged on at the end. I got the vice versa part working by saying:
If inputTextBox.Text Like "--way" Then
MsgBox("Cannot convert, already in English.")
End If
but I'm not sure how to code as to detect if there is NO "--way" anywhere.
Another conversion part is if a word does NOT begin with a, e, i, o or u but contains a, e, i, o, u or y WITHIN the word, those words get:
a dash (-) at the end of the word
the first letter of the word gets moved to the end of the word (this loops until the first letter is a, e, i, o, u or y)
the word gets an "ay" at the end
So Ted becomes "ed-tay."
To sum it all up, my questions are:
1 - How can I set up my Like command to add --way to specific words?
2 - How can I detect for words inside the text box that do NOT have --way in them?
Yes, there is a way to do everything but if you know the right way.
For detecting words in single strings you need to break(divide) the strings in part. In past 2 days, I had worked on this project.
You should first divide the String into columns(pieces), then read each and every word separately. For analysing a string use this sample. Although this method is long you can try other ways as well like you can use array or list. E.g:
`Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim MyWord As String = TextBox1.Text 'This is the input string by user.
Dim writer As New StreamWriter(System.IO.Directory.GetCurrentDirectory + "/fp.txt") 'Filepath = for e.g. = D:\Myfile.txt
Dim i As Double = 0
Dim MyWordlength As Double = MyWord.length
Dim column As String
While i < MyWordlength
column = MyWord.Chars(i)
writer.WriteLine(column)
i = i + 1
End While
writer.close()
writer.Dispose()
' Then set code to analyse
Dim i2 As Double = 0
Dim firstcharacter As String = ReadALine(System.IO.Directory.GetCurrentDirectory + "/fp.txt", GetNumberofLines(System.IO.Directory.GetCurrentDirectory + "/fp.txt"), 0) 'Set File Path (any you wish but valid)
'char.
If firstcharacter = "a" Or firstcharacter = "A" Or firstcharacter = "e" Or firstcharacter = "E" Or firstcharacter = "i" Or firstcharacter = "I" Or firstcharacter = "o" Or firstcharacter = "O" Or firstcharacter = "U" Or firstcharacter = "u" Then
MsgBox("Should not start with a,e,i,o,u.")
End If
'For analysing remaining words.
Dim linec As String
Dim check As Boolean = False
While i2 <= MyWord.Length
linec = ReadALine("D:\Myfile.txt", GetNumberofLines(System.IO.Directory.GetCurrentDirectory + "/fp.txt"), i2)
If linec <> "a" Or linec <> "i" Or linec <> "e" Or linec <> "o" Or linec <> "u" Or linec <> "A" Or linec <> "E" Or linec <> "I" Or linec <> "O" Or linec <> "U" Then
check = False
i2 = i2 + 1
Else
check = True
End If
End While
If check = True Then
Else
MsgBox("You does'not use any vowel between all letters.")
End If
End Sub
Public Function ReadALine(ByVal File_Path As String, ByVal TotalLine As Integer, ByVal Line2Read As Integer) As String
Dim buffer As Array
Dim line As String
If TotalLine <= Line2Read Then
Return "0"
End If
buffer = file.ReadAllLines(File_Path)
line = buffer(Line2Read)
Return line
End Function
Public Function GetNumberofLines(ByVal File_path As String) As Integer
Dim sr As New StreamReader(File_path)
Dim numberoflines As Integer
Do While sr.Peek >= 0
sr.ReadLine()
numberoflines += 1
Loop
Return numberoflines
sr.Close()
sr.Dispose()
End Function
End Class

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

How to indentify the positions that a word occurs in a given text?

I am developing a program where you can input a sentence and then search for a word. The program will then tell you at which positions this word occurs. I have written some code but do not know how to continue.
Module Module1
Sub Main()
Dim Sentence As String
Dim SentenceLength As Integer
Dim L As Integer = 0
Dim LotsofText As String = Console.ReadLine
Console.WriteLine("Enter your word ") : Sentence = Console.ReadLine
For L = 1 To LotsofText.Length
If (Mid(LotsofText, L, 1)) = " " Then
End If
L = L + 1
Dim TextCounter As Integer = 0
Dim MainWord As String = Sentence
Dim CountChar As String = " "
Do While InStr(MainWord, CountChar) > 0
MainWord = Mid(MainWord, 1 + InStr(MainWord, CountChar), Len(MainWord))
TextCounter = TextCounter + 1
'Text = TextCounter + 2
' Console.WriteLine(Text)
Loop
Console.WriteLine(TextCounter)
Console.Write("Press Enter to Exit")
Console.ReadLine()
End Sub
End Module
Transform this piece of code from C# to Visual Basic. match.Index will indicate the position of the given word.
var rx = new Regex("your");
foreach (Match match in rx.Matches("This is your text! This is your text!"))
{
int i = match.Index;
}
To find only words and not sub-strings (for example to ignore "cat" in "catty"):
Dim LotsofText = "catty cat"
Dim Sentence = "cat"
Dim pattern = "\b" & Regex.Escape(Sentence) & "\b"
Dim matches = Regex.Matches(LotsofText, pattern)
For Each m As Match In matches
Debug.Print(m.Index & "") ' 6
Next
If you want to find sub-strings too, you can remove the "\b" parts.
If you add this function to your code:
Public Function GetIndexes(ByVal SearchWithinThis As String, ByVal SearchForThis As String) As List(Of Integer)
Dim Result As New List(Of Integer)
Dim i As Integer = SearchWithinThis.IndexOf(SearchForThis)
While (i <> -1)
Result.Add(i)
i = SearchWithinThis.IndexOf(SearchForThis, i + 1)
End While
Return Result
End Function
And call the function in your code:
Dim Indexes as list(of Integer) = GetIndexes(LotsofText, Sentence)
Now GetIndexes will find all indexes of the word you are searching for within the sentence and put them in the list Indexes.

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)