vb.net searching for a search within a string - vb.net

lot_no = "lot123"
s.indexof("lot123") does not return zero
whereas
s.indexof(lot_no) returns zero
has anyone seen a problem like this?
what does s contain?
For Each s As String In split1

K, looked add the code in your other thread.
When I execute the following code I get a result, what am I doing wrong?
Public lot__no As String = "<Lot no>928374</Lot no>"
Sub DoSomething()
Dim temp_string As String = "<beginning of record>ETCETCETC"
Dim myDelims As String() = New String() {"<beginning of record>"}
Dim Split() As String = temp_string.Split(myDelims, StringSplitOptions.None)
For Each s As String In Split
If InStr(s, lot__no) <> 0 Then
Debug.WriteLine("found" + s)
End If
Next
End Sub

Not sure what you're asking but this code returns -1 / -1
Dim lotnr As String = "lot123"
For Each s As String In "123asd"
Debug.WriteLine(s.IndexOf("lot123"))
Debug.WriteLine(s.IndexOf(lotnr))
Next
Use IndexOf this way:
Dim lotnr As String = "lot123"
For Each s As String In "123asd"
Debug.WriteLine("lot123".IndexOf(s))
Debug.WriteLine(lotnr.IndexOf(s))
Next
This results in:
3
3
4
4
5
5
-1
-1
-1
-1
-1
-1

Related

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

Select the first 2 Characters of each word on String

I created the following function, but have not been able to finish. I want to return the first 2 characters of each word in the string. Here is what I have so far:
Function SelectWords(ByVal text As String, ByVal maxWords As Integer) As String
If String.IsNullOrEmpty(text) Then Return String.Empty
If maxWords <= 0 Then Return String.Empty
Dim words As String() = text.Split(" "c)
Return String ''I am stuck here
End Function
You did not describe the purpose of maxwords, nor what to do with a. The loop part:
Dim words = str.Split(" "c)
Dim ret As New StringBuilder ' in case it is a long string
For Each w As String In words
If w.Length > 1 Then
ret.Append(w.Substring(0, 2))
Else
' decide if you want 1
End If
Next
return ret.toString
The code you have doesn't do anything that you describing .. Try this function instead.
Function SelectWords(ByVal text As String, ByVal maxWords As Integer) As String
Dim collection As MatchCollection = Regex.Matches(text, "(\w{2})\w*\b")
Dim output As New System.Text.StringBuilder
Dim counter As Integer = 0
For Each M As Match In collection
output.Append(M.Groups(1).Value)
counter += 1
If counter = maxWords Then
Exit For
End If
Next
Return output.ToString
End Function

Getting the character that inside the brackets

This is my string:
Dim value as string = "IR_10748(1).jpg"
How can I get this number 1 into another variable? I am thinking to use split.
How I can use to get this value in vb.net?
See String.Substring(Integer, Integer) and String.IndexOf(String).
Dim value As String = "IR_10748(1).jpg"
Dim startIndex As Integer = value.IndexOf("(") + 1
Dim length As Integer = value.IndexOf(")") - startIndex
Dim content As String = value.Substring(startIndex, length)
Regular expressions might be cleaner. This should work:
dim Result as string = Regex.Match(value, "(?<=\().*(?=\))").Value
It'll extract one or more characters contained between the parentheses.
Try this:
Dim value as String = "IR_10748(1).jpg"
Dim splitStrings() as String
'Split on either opening or closing parenthesis -
'This will give 3 strings - "IR_10748","1",".jpg"
splitStrings = value.Split(New String() {"(",")"}, StringSplitOptions.None)
'Parse as integer if required
Dim i as Integer
i = Integer.Parse(splitStrings(1))
Demo
It's not the prettiest, but this gets "1" using Remove and Split :
Dim value as String = "IR_10748(1).jpg"
Dim num As String = value.Remove(0, value.IndexOf("(") + 1).Split(")")(0)
That gives num = "1"
You can get the number more reliably than using String.Split. You'll want to use LastIndexOf to get the final opening parenthesis just in case you have a filename like "a(whatever)(1).ext", and you should inspect the filename without its extension, in case you have a filename like "a(1).(9)":
Dim value As String = "IR_10748(1).jpg"
Dim fn = Path.GetFileNameWithoutExtension(value)
Dim lastOpen = fn.LastIndexOf("(")
If lastOpen >= 0 Then
Dim length = fn.IndexOf(")", lastOpen + 1) - lastOpen - 1
If length >= 1 Then
Dim numAsString = fn.Substring(lastOpen + 1, length)
Console.WriteLine(numAsString)
ElseIf length = 0 Then
' do something if required
Console.WriteLine("No number in the parentheses.")
Else
' do something if required
Console.WriteLine("No closing parenthesis.")
End If
Else
' do something if required
Console.WriteLine("No opening parenthesis.")
End If

VB, how to solve this error

I have some problem in my code :
For i = 0 To Split(awal, vbCrLf).Length - 1
For j = 0 To Split(hasil(i), " ").Length - 1
hasil1(j) = hasil(i).Split(" ")
Next j
Next i
it comes with error :
"Value of type '1-dimensional array of String' cannot be converted to
'String' " in the " hasil1(j) = hasil(i).Split(" ") " section.
can anyone explain to me how to solve this, please?
It is not clear how you initialize your hasil array,
however, you could simplify your code using a List(Of String) in this way
Dim hasil() as String = New String() {"abcd 123","efgh 11","ijkl"}
Dim hasil1 as List(Of String) = New List(Of String)()
For i = 0 To hasil.Length - 1
hasil1.AddRange(hasil(i).Split(" "))
Next i
You can allways convert it back to a string array with:
Dim s() as String = hasil1.ToArray()
You problem is that you are trying to add an Array to a String Array.
The Split() function returns an Array and this is causing the error.
If your intention is to get an Array of Arrays, make that variables that way, if not, what was the purpose on using Split(" ") on that line?
Edit:
Based on you comment:
You will have to loop once more for this to be complete:
Dim it As Integer
Dim tmp() As String
it = 0
For i = 0 To Split(awal, vbCrLf).Length - 1
For j = 0 To Split(hasil(i), " ").Length - 1
tmp = hasil(i).Split(" ")
For k = 0 To tmp.Length - 1
hasil1(it) = tmp(k)
it += 1
Next
Next j
Next i
My skills in VB.Net are rusted and I'm not in my dev machine, so try this and let me know if something doesn't work.

How to get an array to display all its values at once

Here is some sample code:
Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1
How can I display those four values next to each other.
More specifically, given those values how can I make txtValue.Text = 5471
Edit:
The idea I had would be to use some sort of function to append each one to the end using a loop like this:
Dim finalValue
For i As Integer = 3 To 0 Step -1
arrValue(i).appendTo.finalValue
Next
Obviously that code wouldn't work though the premise is sound I don't know the syntax for appending things and I'm sure I wouldn't be able to append an Integer anyway, I would need to convert each individual value to a string first.
Another method is to use String.Join:
Sub Main
Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1
Dim result As String = String.Join("", arrValue)
Console.WriteLine(result)
End Sub
If I understand your question correctly, you can use StringBuilder to append the values together.
Dim finalValue as StringBuilder
finalValue = new StringBuilder()
For i As Integer = 3 To 0 Step -1
finalValue.Append(arrValue(i))
Next
Then just return the finalValue.ToString()
Convert the integers to strings, and concatenate them:
Dim result as String = ""
For Each value as Integer in arrValue
result += value.ToString()
Next
Note: using += to concatenate strings performs badly if you have many strings. Then you should use a StringBuilder instead:
Dim builder as New StringBuilder()
For Each value as Integer in arrValue
builder.Append(value)
Next
Dim result as String = builder.ToString()
for i = lbound(arrValue) to ubound(arrValue)
ss=ss & arrValue(i)
next i
end for
debug.print ss
Dim value as string = ""
For A As Integer = 1 To Begin.nOfMarks
value += "Mark " & A & ": " & (Begin.Marks(A)) & vbCrLf
Next A