trying to get emails list from RichTextBox to be in listbox.
so i used (for each, StringSplitOptions, to split the full text)
Richtextbox : "one#gmail.com two#yahoo.com three#hotmail.com that's all"
Code
For Each str As String In RichTextBox1.Text.Split(New String() {"#"}, StringSplitOptions.None)
ListBox1.Items.Add(str.Substring(str.LastIndexOf(" ") + 1))
Next
result in listbox :
one
two
three
all
if u search on "#Gmail.com"
you can change the loop to integer
for i = 0 to Length - 1
and use substring
(str.substring(str.indexof(" "), str.indexof(" .com")), or just add +#gmail.com
Well its normal cause u getting splits #, So u need to use Regex
but in that case u find all mail forum, so i recommended, to get valid email list, search on (# and also .) which contain emails list.
Dim reg As Regex = New Regex("(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))#" &
"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." &
"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" &
"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
For Each email As Match In reg.Matches(RichTextBox1.Text)
ListBox1.Items.Add(email.Value.ToString())
Next
MessageBox.Show("Done")
Hope it work
Related
I have a database with a table called "sales person" which has a combination of names & surnames. On my report it must include the shortened name. Just the most left character of the Names and Surname combined. For example some has just one Name and a Surname, eg. "Pete Sampras". Combined it would show "PS" on my report. Some have more, like "Pete Steff Sampras". Combined it would be "PSS". For my own name is Johan vd Westhuizen. It must now look like "JVW". How would I go about it?
I am a beginner at this, and I'm not sure what to use. I have tried left(), but that's only for the first name
You can split the word in the spaces, and then use the Left() function to get the first character for each word.
In addition, convert it to upper case and trim to remove any spaces (I don't expect any but just in case).
See an example:
Public Function GetInitialsFromName(ByVal fullname As String) As String
'Array to hold the words
Dim arr As Variant
arr = Split(fullname, " ")
Dim initials As String, idx As Integer
'Loop each word, take the 1st letter and append it to the initials.
'Trim and convert to upper case.
For idx = LBound(arr) To UBound(arr)
initials = initials & StrConv(Left(Trim(arr(idx)), 1), vbUpperCase)
Next
GetInitialsFromName = initials
End Function
To call it:
Debug.Print GetInitialsFromName("Johan vd Westhuizen")
Debug.Print GetInitialsFromName("Pete Steff Sampras")
Debug.Print GetInitialsFromName("Pete Sampras")
'JVW
'PSS
'PS
Trying not to make this more complicated than it needs to be.
But I need to have a textbox and a button- when I click the button the code should count the words(i did this) and also display each word and how many times it appears in the string(aka textbox)
For example- Hello my name is Tom, Tom is good
Listbox
Hello 1
My 1
Name 1
is 2
Tom 2
good 1
Any help is appreciated, thanks(this is not for a test etc)
An alternate approach to indexOf would be to use a Dictionary to maintain the words and the counts. The benefit would be for large strings there would be less iterations than using indexOf as you could do something like:
1) Split the string (assuming this is just one loop through your string)
2) Iterate over the array once to count the number of occurrences of each word
Whereas with indexOf you'd need a fair few iterations to get through everything, depending on the size of your string
Dictionaries/Maps are nice data structures to know, in general. Here is a naive example as a console application:
Dim wordCount As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
Dim exampleString As String = "Hello my name is Tom, Tom is good"
Dim seperator() As String = {" ", ","}
Dim splitString() As String = exampleString.Split(seperator, StringSplitOptions.RemoveEmptyEntries)
For Each s As String In splitString
If wordCount.ContainsKey(s) Then
wordCount(s) = wordCount(s) + 1
Else
wordCount(s) = 1
End If
Next
For Each s As KeyValuePair(Of String, Integer) In wordCount
Console.WriteLine(s.Key + " " + s.Value.ToString())
Next
Console.ReadLine()
But the code does look a bit more complicated, I guess.
You could use VB.net IndexOf function with a loop to find the multiple instances of every word in the string and count them.
Just use a loop with the indexof function for every word you find in the whole string entered in the textbox.
I am trying to split a string with multiple characters. The string might sometimes contain a - or a /. What I have achieved is the hyphen but I am not able to search for the slash. Any thoughts on how to split the string based on both characters at once ? Once I split after - I add the value after the - to the result list as a separate index and I would like to accomplish the same for '/'.
So For example the Split string has Jet-blue, the below code will add Jet in the result list with index(0) and blue with index(1). In addition to splitting with '-' I would also like to split with '/'. Any suggestions ?
Code:
Dim result As New List(Of String)()
For Each str_get As String In Split
Dim splitStr = str_get.Split("-")
For Each str_split As String In splitStr
result.Add(str_split) ' Enter into result list
' result.TrimExcess()
Next
result.Remove("")
Next
You can either use this or this overload of the Split method.
The first one takes an array of Char:
"Hello World".Split({"e"c, "o"c}) ' Notice the c!
The second one takes an array of String and StringSplitOptions:
"Hello World".Split({"el", "o"}, StringSplitOptions.None)
I have two lines of text which have long space (more like 14-15 spaces) before the actual text. I have tried simple replace to split and merge but nothing is working. I have also tried trim and the worst thing is that ASCII gives code of 32. But nothing works. Here is the text :
your heartburn symptoms
Certain foods, such as fat, chocolate, caffeine and alcohol can aggravate heartburn symptoms 1
Certain foods
(BTW it's not like it looks it is. In my actual richtextbox, when I select the space it gets selected as one big piece of space like a tab and i have also tried replacing vbtab but no use)
What I want is :
your heartburn symptoms
Certain foods, such as fat, chocolate, caffeine and alcohol can aggravate heartburn symptoms 1
Certain foods
Believe me I have tried almost 7-8 diffferent function but now I am going mad. Some of my logic :
Dim lineArray As String() = rtfArticle.Lines
For z As Integer = 0 To lineArray.Length - 1
Dim w As String() = lineArray(z).Split(" ")
MsgBox(lineArray(z))
Dim tmp As String = ""
For Each s34 As String In w
If (s34 <> " ") Then
temp = temp & " " & s34
End If
Next
lineArray(z) = temp
Next
It completely messes up the code. Any idea about this?
You could try:
Dim lineArray As String() = rtfArticle.Lines
For z As Integer = 0 To lineArray.Length - 1
lineArray(z) = lineArray(z).Trim()
Next
MSDN for Trim() says:
Removes all leading and trailing white-space characters from the
current String object.
Ive searched over and over the internet for my issue but I havent been able to find / word my searches correctly...
My issue here is that I have a Comma Separated value file in .txt format... simply put, its a bunch of data delimited by commas and text qualifier is separated with ""
For example:
"So and so","1234","Blah Blah", "Foo","Bar","","","",""
"foofoo","barbar","etc.."
Where ever there is a carriage return it signifies a new row and every comma separates a new column from another.
My next step is to go into VB.net and create an array using these values and having the commas serve as the delimeter and somehow making the array into a table where the text files' format matches the array (i hope im explaining myself correctly :/ )
After that array has been created, I need to select only certain parts of that array and store the value into a variable for later use....
Andthats where my trouble comes in... I cant seem to get the correct logic as to how to make the array and selecting the certain info out of it..
If any
You might perhaps give a more detailed problem description, but I gather you're looking for something like this:
Sub Main()
Dim fileOne As String = "a1,b1,c1,d1,e1,f1,g1" + Environment.NewLine + _
"a2,b2,c2,d2,e2,f2,g2"
Dim table As New List(Of List(Of String))
' Process the file
For Each line As String In fileOne.Split(Environment.NewLine)
Dim row As New List(Of String)
For Each value In line.Split(",")
row.Add(value)
Next
table.Add(row)
Next
' Search the "table" using LINQ (for example)
Dim v = From c In table _
Where c(2) = "c1"
Console.WriteLine("Rows containing 'c1' in the 3rd column:")
For Each x As List(Of String) In v
Console.WriteLine(x(0)) ' printing the 1st column only
Next
' *** EDIT: added this after clarification
' Fetch value in row 2, column 3 (remember that lists are zero-indexed)
Console.WriteLine("Value of (2, 3): " + table(1)(2))
End Sub