Vb.Net LINQ Replace pattern - vb.net

I want to create a sort of Input Mask from Access. I want the user to input romanian phone numbers like 07XX XXX XXX. The thing is that it needs to be a string beacuse some phone numbers will start with a + sign, so number formats can't be applied (to my knowledge)
What i have tried:
Dim d As String = "---- --- ---"
For Each m As Match In Regex.Matches(TelRe, "[\S+]")
d = Strings.Replace(d, "-", m.Value, 1, 1)
Next
sender.text = d
What i need:
What i would like to know, as i am trying to learn LINQ, is there a shorter, prettier LINQ function to use with that code?
Thank you

Related

Using StringSplitOptions to get word list VB.NET

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

Input sequence / Output number VB.Net

I currently have a question with an activity which I am attempting.
The question is as follows:
Steve wants to enter all his staff member's names into a system. He has the following criteria however:
Must be last names.
Must be in the correct format.
Must calculate and output the number of surnames.
The format must be as the below example:
**stevens#smith#thomas#mitchell#adams**
To summarise:
The program must input the character sequence (made up of one or more surenames)
Check that the character sequence has a valid format.
Calculate and output the number of surnames.
This is what I have done so far - however I was wondering if there were any way that I could remove the array and make the code simpler and easier to remember. Any ideas are much appreciated!
Module Module1
Sub Main()
Dim sequence As String
Dim surnamecount As Integer = 0
Dim sequencearray() As String
Console.WriteLine("Please enter a character sequence")
sequence = Console.ReadLine
If Len(sequence) >= 5 And sequence.Substring(0, 2) = "**" And sequence.Substring() Then
sequencearray = Split(sequence, "#")
sequencearray(0) = sequencearray(0).Substring(2)
sequencearray(UBound(sequencearray)) = sequencearray(UBound(sequencearray)).Substring(0, Len(sequencearray(UBound(sequencearray))) - 2)
For i As Integer = 0 To UBound(sequencearray)
Console.WriteLine(sequencearray(i))
surnamecount = surnamecount + 1
Next
Console.ReadLine()
End Sub
Thank you in advance!

Get the nth character, string, or number after delimiter in Visual Basic

In VB.net (Visual Studio 2015) how can I get the nth string (or number) in a comma-separated list?Say I have a comma-separated list of numbers like so:13,1,6,7,2,12,9,3,5,11,4,8,10How can I get, say, the 5th value in this string, in this case 12?I've looked at the Split function, but it converts a string into an array. I guess I could do that and then get the 5th element of that array, but that seems like a lot to go through just to get the 5th element. Is there a more direct way to do this, or am I pretty much limited to the Split function?
In case you are looking for an alternative method, which is more basic, you can try this:
Module Module1
Sub Main()
Dim a As String = "13,1,6,7,2,12,9,3,5,11,4,8,10"
Dim counter As Integer = 5 'the number you want (in this case, 5th one)
Dim movingcounter As Integer = 0 'how many times we have moved
Dim startofnumber, endofnumber, i As Integer
Dim numberthatIwant As String
Do Until movingcounter = counter
startofnumber = InStr(i + 1, a, ",")
i = startofnumber
movingcounter = movingcounter + 1
Loop
endofnumber = InStr(startofnumber + 1, a, ",")
numberthatIwant = (Mid(a, startofnumber + 1, endofnumber - startofnumber - 1))
Console.WriteLine("The number that I want: " + numberthatIwant)
Console.ReadLine()
End Sub
End Module
Edit: You can make this into a procedure or function if you wish to use it in a larger program, but this code run in console mode will give the output of 12.
The solution provided by Plutonix as a comment to my question is straightforward and exactly what I was looking for, to wit:result = csv.Split(","c)(5)In my case I was incrementing a variable each time my program ran and needed to get the nth character or string after the incremented value. That is, if my program had incremented the variable 5 times, then I needed the string after the 4th comma, which of course, is the 5th string. So my solution was something like this:result = WholeString.Split(","c)(IncrementedVariable)Note that this is a zero-based variable.Thanks, Plutonix.

using IndexOf in Mid function

Perhaps this is a simple solution for most, but I can't get this to work like it should according to syntax.
I have this line of text "Part Number123456Price$50.00"
I want to pull the part number out of it, so I use this function...
str = Mid(str, str.IndexOf("Part Number") + 12, str.IndexOf("Price"))
My results are str = "123456Price$50.0" every time. I know the part number can vary in length so I need a solid solution of pulling this out.
It can be confusing to mix the legacy VB string methods (such as Mid) with the .Net string methods (like IndexOf). The VB methods use 1 as the index of the first character while the .Net methods use 0.
The following code will extract the part number from a string
Dim str As String = "Part Number123456Price$50.00"
Dim iPart As Integer = str.IndexOf("Part Number") + 11
Dim iPrice As Integer = str.IndexOf("Price")
str = str.Substring(iPart, iPrice - iPart).Trim
The Mid() function of Visual Basic is documented as having three arguments: (1) a string, (2) the beginning location in the string, and (3) the number of characters to copy.
So if your string is "Part Number123456Price$50.00" and you want to pull the part number as a series of digits, the "123456" part of the string, using the Mid() function then you need to find the beginning of the part number digit string and to then know the number of digits.
If your string is in the variable str then you can find the offset by something like str.IndexOf("Number") + len("Number") which will provide the offset to after the string "Number".
Next you need to find the number of digits so you would do something like str.IndexOf("Price") to find where the text "Price" begins and then subtract from that offset the offset of where the digits begin.
The result of all of this is you need a bit of code something like the following. I have not tested this source as I am not a VB programmer so it may need a tweak and you might want to put some checks on data validity as well.
Dim TextNumber as String = "Number"
Dim TextPrice as String = "Price"
iOffset = str.IndexOf(TextNumber) + len(TextNumber)
str = Mid(str, iOffset, str.IndexOf(TextPrice) - iOffset)
Alternatively, if Price is always the format $00.00, this will also work.
Dim str as String = "Part Number123456Price$50.00"
str = str.Remove(str.IndexOf("Price"))

Get substring until first numeric character

like my title already explained, I want to get a substring of a string (who contains a address) and I would like to have only the street..
It's not possible to only take the text (non-numeric) chars, because then the box will remain.
It's not possible to take substring till first space, because the streetname can contain a space..
For example 'developerstreet 123a' -> would like to have 'developerstreet'
The 'a' is a box number of the house, which I'm not interested in..
How can I do this in VB.NET?
Parsing addresses is notoriously difficult, so I caution you to make sure that you a very deliberate about the choices you make. I would strongly recommend reviewing the documentation provided by the postal service. If these are US addresses, you should start by looking at the USPS Publication 28.
However, to answer your specific question, you can find the index of the first numeric character in a string by using the Char.IsDigit method. You may also want to take a look at the Char.IsNumber method, but that's probably more inclusive than what you really want. For instance, this will get the index of the first numeric character in the input string:
Dim index As Integer = -1
For i As Integer = 0 to input.Length - 1
If Char.IsDigit(input(i)) Then
index = i
Exit For
End If
Next
However, for complex string parsing, like this, I would suggest learning Regular Expressions. Getting the non-numeric portion at the beginning of a string becomes trivial with RegEx:
Dim m As Match = Regex.Match(input, "^\D+")
If m.Success Then
Dim nonNumericPart As String = m.Value
End If
Here is the meaning of the regular expression in the above example:
^ - The matching string must start at the beginning of the line
\D - Any non-numeric character
+ - One or more times
try this:
Private Sub MyFormLoad(sender As Object, e As EventArgs) Handles Me.Load
Dim str As String = "developerstreet 123a"
Dim index As Integer = GetIndexOfNumber(str)
Dim substr As String = str.Substring(0, index)
MsgBox(substr)
End Sub
Public Function GetIndexOfNumber(ByVal str As String)
For n = 0 To str.Length - 1
If IsNumeric(str.Substring(n, 1)) Then
Return n
End If
Next
Return -1
End Function
output will be: developerstreet
text.Substring(0, text.IndexOfAny("0123456789"))