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)
Related
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 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
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
Okay, so I am trying to make my program take whatever your key is, and loop through each character of the key, then find the ascii code for each character code, and then loop through each character of the message, finding the ascii code of each of them, and adding the key code to the message code, doing this for each character in the key, to each character in the message. I ran into a little problem, it changes the message right after the first letter is added, and I can't figure out how to fix it, any help would be great!
Basically, all I want is to that the ascii code for the key characters and add them to the ascii code for the message characters, then convert that final code back to the new characters in the message text. Using this:
tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))
Here is everything I've got so far:
Public Class Form1
Function fctEncryptDecrypt(pMess As String, pKey As String) As String
If Len(tbxMessage.Text) > 0 Then
Dim xForKey As Integer
Dim xForMess As Integer
Dim intKey As Integer
Dim intMessage As Integer
Dim strAsciiKeyChar As String
Dim intAsciiKeyChar As Integer
Dim strAsciiMesChar As String
Dim intAsciiMesChar As Integer
Dim vTemp As String
Dim vTempMess As String
intKey = Len(tbxKey.Text)
intMessage = Len(tbxMessage.Text)
For xForKey = 1 To intKey
strAsciiKeyChar = Mid(tbxKey.Text, xForKey, 1)
intAsciiKeyChar = AscW(strAsciiKeyChar)
vTemp = intAsciiKeyChar
For xForMess = 1 To intMessage
strAsciiMesChar = Mid(tbxMessage.Text, xForMess, 1)
intAsciiMesChar = AscW(strAsciiMesChar)
vTempMess = vTemp + intAsciiMesChar
tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))
Next xForMess
Next xForKey
Label1.Text = vTemp
Else
MessageBox.Show("No Message Found")
End If
End Function
Private Sub btnEncrypt_Click(sender As System.Object, e As System.EventArgs) Handles btnEncrypt.Click
fctEncryptDecrypt(tbxMessage.Text, tbxKey.Text)
End Sub
End Class
tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))
in the inner For loop is setting the value of the text box to a number.
I'd expect the use of a temporary string variable that collects
Chr((intAsciiKeyChar + intAsciiMesChar) mod 256)
I'd also expect the key to be applied one letter at a time over the message. Something like:
Dim i as Integer
Dim s as String
Dim sKey as String
Dim sMesg as String
Dim intCharacter as Integer
s = ""
For i = 1 to len(tbxMessage.Text)
sKey = Mid(tbxKey.Text, (i mod Len(tbxKey)) + 1, 1)
sMesg = Mid(tbxMessage.Text, i, 1)
intCharacter = (WAsc(sKey) + WAsc(sMesg)) mod 256
s = s & Chr(intCharacter)
Next
tbxMessage.Text = s
I currently have a file with 1 million characters.. the file is 1 MB in size. I am trying to parse data with this old function that still works but very slow.
start0end
start1end
start2end
start3end
start4end
start5end
start6end
the code, takes about 5 painful minutes to process the whole data.
any pointers and suggestions are appreciated.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sFinal = ""
Dim strData = textbox.Text
Dim strFirst = "start"
Dim strSec = "end"
Dim strID As String, Pos1 As Long, Pos2 As Long, strCur As String = ""
Do While InStr(strData, strFirst) > 0
Pos1 = InStr(strData, strFirst)
strID = Mid(strData, Pos1 + Len(strFirst))
Pos2 = InStr(strID, strSec)
If Pos2 > 0 Then
strID = Microsoft.VisualBasic.Left(strID, Pos2 - 1)
End If
If strID <> strCur Then
strCur = strID
sFinal += strID & ","
End If
strData = Mid(strData, Pos1 + Len(strFirst) + 3 + Len(strID))
Loop
End Sub
The reason that is so slow is because you keep destroying and recreating a 1 MB string over and over. Strings are immutable, so strData = Mid(strData... creates a new string and copies the remaining of the 1 MB string data to a new strData variable over and over and over. Interestingly, even VB6 allowed for a progressive index.
I would have processed the disk file LINE BY LINE and plucked out the info as it was read (see streamreader.ReadLine) to avoid working with a 1MB string. Pretty much the same method could be used there.
' 1 MB textbox data (!?)
Dim sData As String = TextBox1.Text
' start/stop - probably fake
Dim sStart As String = "start"
Dim sStop As String = "end"
' result
Dim sbResult As New StringBuilder
' progressive index
Dim nNDX As Integer = 0
' shortcut at least as far as typing and readability
Dim MagicNumber As Integer = sStart.Length
' NEXT index of start/stop after nNDX
Dim i As Integer = 0
Dim j As Integer = 0
' loop as long as string remains
Do While (nNDX < sData.Length) AndAlso (i >= 0)
i = sData.IndexOf(sStart, nNDX) ' start index
j = sData.IndexOf(sStop, i) ' stop index
' Extract and append bracketed substring
sbResult.Append(sData.Substring(i + MagicNumber, j - (i + MagicNumber)))
' add a cute comma
sbResult.Append(",")
nNDX = j ' where we start next time
i = sData.IndexOf(sStart, nNDX)
Loop
' remove last comma
sbResult.Remove(sbResult.ToString.Length - 1, 1)
' show my work
Console.WriteLine(sbResult.ToString)
EDIT: Small mod for the ad hoc test data