Only get characters surrounded by " " - vb.net

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

Related

Detect Unprintable ASCII Characters in String

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

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

Not read text file correct

I want when I read txt file and add in ListBox1.items, add this text http://prntscr.com/on12e0 correct text §eUltra §8[§716x§8].zip not like this http://prntscr.com/on11kv
My code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
My.Computer.FileSystem.CopyFile(
appDataFolder & "\.minecraft\logs\latest.log",
appDataFolder & "\.minecraft\logs\latestc.log")
Using reader As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(" Reloading ResourceManager: Default,") Then
Dim lastpart As String = line.Substring(line.LastIndexOf(", ") + 1)
ListBox1.Items.Add(lastpart)
End If
End While
End Using
My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")
End Sub
This question is only different from your first question in that your have substituted a ListBox for a RichTextBox. It seems you got perfectly acceptable answers to your first question. But I will try again.
First get the path to the file. I don't know why you are copying the file so I didn't.
Add Imports System.IO to the top of your file. The you can use the File class methods. File.ReadAllLines returns an array of strings.
Next use Linq to get the items you want. Don't update the user interface on each iteration of a loop. The invisible Linq loop just adds the items to an array. Then you can update the UI once with .AddRange.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\.minecraft\logs\latest.log")
Dim lines = File.ReadAllLines(appDataFolder)
Dim lstItems = (From l In lines
Where l.Contains(" Reloading ResourceManager: Default,")
Select l.Substring(l.LastIndexOf(", ") + 1)).ToArray
ListBox1.Items.AddRange(lstItems)
End Sub
If this answer and the previous 2 answer you got don't work, please lets us know why.

How to read contents from a file, do some processing, then write to a text box

It's been nearly 20 years since I last used VB.NET and I've forgotten everything :( My project:
Read the contents of a text file and write to a text box
If the line contains a specified string, write to a second text box
Repeat until end of file
What I have so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.SourceText.LoadFile("e:\xdcc.txt", RichTextBoxStreamType.PlainText)
Dim Term As String = "Santa"
For Each Line As String In IO.File.ReadLines("e:\xdcc.txt")
If Line.Contains(Term) = True Then
OutputText.Text = (Line) & vbNewLine
End If
Exit For
Next
End Sub
Problem:
I can read the contents to the first text box and I can output a matching line to the second text box. It's not parsing the rest of the file and writing all the subsequent matches.
Thank you
I believe there are two mistakes here:
first, your Exit For statement is causing the For loop to end on the first iteration;
Second, as I understood in your question you want to show in the text box all of the matches found inside the file. You need to concatenate the 'Current text' with the next match.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.SourceText.LoadFile("e:\xdcc.txt", RichTextBoxStreamType.PlainText)
Dim Term As String = "Santa"
For Each Line As String In IO.File.ReadLines("e:\xdcc.txt")
If Line.Contains(Term) = True Then
'OutputText.Text = (Line) & vbNewLine
OutputText.Text &= (Line) & vbNewLine
End If
'Exit For
Next
End Sub
That might solve your problem.
Remove the Exit For. that is causing the loop to end after the first round about
Think I figured it out. I removed that Exit For statement and in the If section I changed it to:
OutputText.Text = OutputText.Text & (Line) & vbNewLine
I now get the results that I was looking for

creating text file in VB.NET adding extra line

I am trying to create text file with the code below; but there is always an extra line at the end that I cannot get rid of. Is there any way to modify this code to write the text file without the last line?? Help appreciated:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MekdamFile As String
MekdamFile = "C:\TEMP\MEKDAM.txt"
Dim dones As New List(Of String)
For i = 1 To 10
dones.Add("test test " & i)
Next
Using sw As New StreamWriter(MekdamFile)
For Each i As String In dones
sw.WriteLine(i)
Next
End Using
End Sub
End Class
Thanks for help in advance...
StreamWriter.WriteLine always writes a line terminator.
On the last iteration of the loop you could use sw.Write instead.
Your code can be simplified a bit:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
File.WriteAllText("C:\TEMP\MEKDAM.txt", [String].Join( _
Environment.NewLine, _
Enumerable.Range(1, 10).[Select](Function(i) "Test test" + Cstr(i))))
End Sub
You are calling WriteLINE, so whatever you pass into it will be appended with a newline character sequence.
If you don't want to write the newline, use Write instead of WriteLine. Of course, you should then append the newline yourself for all lines except the last.
Alternatively, for a small text, just build the whole text, then trim the end of it and write it at once:
Dim sb As new StringBuilder();
For i = 1 To 10
sb.AppendLine("line " + i);
Next
File.WriteAllText(path, sb.ToString().TrimEnd());