Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys, so i have a text box that the user will enter an alphanumeric code, however the database that i will use to store data will depend on the alphabet code ONLY inside this alphanumeric code. Can you help me how to start on this one please?

I.e. code entered on textbox1 is MSE001243, i need to extract the MSE and put it on a label that i can use to access the corresponding database.

Thankyou!

What I have tried:

I've seen seen validations but not extractions on the web :(
Posted
Updated 11-Nov-20 22:55pm

You can loop through the chars in the string and use Char.IsLetter to check whether the character is a letter.
Using the Where extension and the String constructor that takes an array of Char, the code would look as follows.
VB
Dim letters = "MSE001243".Where(Function(c) Char.IsLetter(c)).ToArray()
Dim word = New String(letters)
' word is now MSE
Alternatively, you could use your pre-defined set of characters and use Contains instead.
VB
Dim allowed = "abcde...ABCDE..."
Dim letters = "MSE001243".Where(Function(c) allowed.Contains(c)).ToArray()
 
Share this answer
 
v2
Comments
Maciej Los 12-Nov-20 6:21am    
5ed!
Member 14122214 13-Nov-20 3:26am    
Dim letters = "MSE001243".Where(Function(c) Char.IsLetter(c)).ToArray()
Dim word = New String(letters)
' word is now MSE 


this worked like a charm, how can i change "MSE001243" to textbox1.tex? i tried using
"& textbox1.text &".Where(Function(c) Char.IsLetter(c)).ToArray() 


but it doesnt work as what i need is for the system to get the letter of whatever i type in textbox1.text
Member 14122214 13-Nov-20 3:39am    
tried using this as well

 Dim sample As String = TextBox1.Text
        Dim letters = sample.Where(Function(c) Char.IsLetter(c)).ToArray()
        Dim word = New String(letters)
        Label3.Text = word 


but also doesnt work
Sander Rossel 13-Nov-20 4:07am    
This should work, assuming TextBox1 and Label3 are the correct TextBox and Label.
What doesn't work?
Do you get an error?
Member 14122214 15-Nov-20 20:34pm    
sorry, i just missed callingmy private sub on the event haha.. all works now, thank you!
Use a regex:
VB
Dim input As String = "MSE001243, i need"
Dim output As String = Regex.Replace(input, "[^a-zA-Z]", "")
 
Share this answer
 
Comments
Sander Rossel 12-Nov-20 4:59am    
"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." - Jamie Zawinski :)
A valid solution though.
OriginalGriff 12-Nov-20 5:29am    
:D
Maciej Los 12-Nov-20 6:21am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900