Detect Unprintable ASCII Characters in String - vb.net

I'm using Visual Basic 2010 Express. I need to parse a string with unprintable characters in it. I need to detect ASCII 4 (End of Trans).
A scanner dumps data into a TextBox in my app. In a loop, I am using:
If Chr(MyString.Chars(counter)) = 4 Then
MsgBox("Found")
End If
This is not the correct syntax but should convey what I'm trying to do.

After the scanner dumps the data into a textbox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Some chars coming in from " & Chr(4) & " a scanner"
End Sub
Try something like this:
Dim MyString As String = TextBox1.Text
If MyString.Contains(Chr(4)) Then
MessageBox.Show("Found")
End If
Or even something like this:
Dim MyString As String = TextBox1.Text
Dim counter As Integer = 26
If MyString.Chars(counter) = Chr(4) Then
MessageBox.Show("Found")
End If

Related

I want to replace some characters from a string. I used the Replace command. But this is not working

I am writing some code in VB.Net to subtract one string from another string, but this is not working. in output nothing is changed in the target string. But there is no error message. Please help. Thanks.
If RadioButton1.Checked Then
TextBox1.Text = ""
positive = (TextBoxp1.Text + TextBoxp2.Text + TextBoxp3.Text)
negative = (TextBoxn1.Text + TextBoxn2.Text + TextBoxn3.Text)
findstring = Replace(positive, negative, "")
TextBox1.Text = findstring
End If
The concatenation symbol in vb.net is the ampersand (&). You may get unexpected results it you use the plus sign and the strings contain numbers. Parenthesis are not necessary to evaluate an expression except to establish order of calculation when it conflicts with order of precedence.
You are using the vb.net Strings.Replace method. I would use the .net String.Replace method because it is easier to move between .net languages when you get used to using .net methods instead of vb specific methods.
This method takes the original string in this case negative and looks for the entire positive string. If it finds the entire string it replaces it with the empty string.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim positive = "b" & "cd" & "ef"
Dim negative = "abc" & "def" & "ghi"
TextBox1.Text = negative.Replace(positive, "")
'Result is aghi
End Sub
If you are trying to remove individual letters from a string then you will have to use a loop. Luckily for us a String is an array of Char.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim positive = "ceg"
Dim negative = "abcdefg"
For Each ch As Char In positive
negative = negative.Replace(ch, "")
Next
TextBox1.Text = negative
'Result abdf
End Sub
You are making this way too complicated. If what you want is to remove a substring from within a string use replace like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = Replace(txtLargeString.Text, txtSearchString.Text, "")
End If
End Sub
All you need is two radio buttons, three text boxes and a button. If you enter 1121221114141 in the txtLargeString text box, 2122 in the txtSearchString text box and execute the code, the result is 111114141 which is the result of removing the txtSearchString input from the txtLargeString input.
Or if as #Mary suggested you want to use the more modern version of replace use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = txtLargeString.Text.Replace(txtSearchString.Text, "")
End If
End Sub

Eliminate duplicate from a Multi-Line string

I have a text-box control called Textbox1 which contains the following items (in a Multi-line string):
22,23
57,58
20,21
51,52
57,58
20,21
21,22
25,26
35,36
41,42
50,51
22,23
23,24
37,38
44,45
45,46
67,68
72,73
78,79
How do I remove 2-digit duplicates? Instead of 20,21, it does not appear twice and once. If the combination of 2 numbers still exists in another line, then this combination appears once. and so on.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines() As String = TextBox1.Text.Split(New String() {vbCrLf, vbCr, vbLf}, StringSplitOptions.None)
Dim repeatedElement As String = ""
'First have to sort array
System.Array.Sort(lines)
For Each Line As String In lines
If Not (String.Compare(Line, repeatedElement) = 0) Then
TextBox2.Text += Line & vbCrLf
End If
repeatedElement = Line
Next
End Sub
Here textBox1 is for source text and textBox2 is for result.

how to read input from barcode scanner and only display required content?

Example: barcode content is 1|123456|78910 but i want to retrieve only 123456
Here is my code: Currently my code retrieve all the barcode content.
Private Sub TextVendorID_TextChanged(sender As Object, e As EventArgs) Handles
TextVendorID.TextChanged
Dim s As String = TextVendorID.Text
s = Mid(s, 3, 5)
End Sub
Try this:
This code will do the job, assuming that all barcodes have three parts separated by a "|" character.
The length of the vendor ID (I assume it is the middle string between the "|" character) is not relevant.
Your input string will be split into an array based on the "|" character. strSplit(0) will = 1, strSplit(1) will = 123456 and strSplit(2) will = 78910. The pipes ("|") will be dropped.
Private Sub TextVendorID_TextChanged(sender As Object, e As EventArgs) Handles TextVendorID.TextChanged
Try
Dim strSplit() As String = TextVendorID.Text.Split("|")
If strSplit.Count > 2 Then
TextVendorID.Text = strSplit(1)
End If
Catch ex As Exception
End Try
End Sub

Only get characters surrounded by " "

I'm Currently writing a program that will convert my code into Pseudo Code but I'm having the problem that I cannot figure out how to get Only text that is surrounded by " " and comes after the word print.
Currently have
Dim str() As String = TextBox1.Text.Split()
If str.Contains("Print") Then
End If
Nothing
Here is one way. Instead of splitting and then checking for Print, Check for Print first and then split it.
Tried and Tested
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If TextBox1.Text.Contains("Print") Then
Dim ar As String = TextBox1.Text.Substring(TextBox1.Text.IndexOf("Print"))
Dim splitted() As String = Split(ar, """")
If splitted.Length > 1 Then MessageBox.Show(splitted(1))
End If
End Sub
If the string is say This "is" a great Print Job! "Nice Work" then the above code should give you Nice Work

Too many arguments to `LTrim`

I am getting an error in my program.
Too many arguments to Public Function LTrim(str As String) As String.
Dim fortrim As String
Dim trimed As String
fortrim = TextBox1.Text
trimed = LTrim(fortrim, 3)
' ^
' error appears here
TextBox2.Text = trimed
Help is appreciated. I can't find a workaround.
I really suggest you to remove the old VB6 functions and use the more advanced NET equivalent.
string.TrimStart
string.TrimEnd
string.Trim
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = TextBox1.Text.TrimStart(Nothing)
End Sub
EDIT: Instead, (looking at your comment in a different answer) if you want to extract the last 3 chars from a string the method to use is string.Substring and the property string.Length
Dim original = TextBox1.Text
Dim last3 = original
' be sure to have at least 3 chars before doing substring math
If last3.Length > 3 Then
last3 = original.Substring(original.Length - 3, 3)
End If
TextBox2.Text = last3
Well, as it states, you have more arguments than what is expected.
So change
LTrim(fortrim, 3)
to
LTrim(fortrim)
You could also shorten this code to something like
TextBox2.Text = LTrim(TextBox1.Text)