Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys,

please don't ask why i am doing this but i am trying to set a string value with the user input. this is an example code of what the user input will look like:

name(0,0) = Hello
name(1,0) = World
name(2,0) = Something

so when you type in the 'name(0,0)' into the textbox(textbox1) it will recognize the string name(0,0). i know how to do the value setting but i don't know how to get the program to recognise that you put in the string name. i don't want to code this as:

VB
for each a as string in textbox1.text.split(system.environment.newline())
     if a.contains("name(1,0) = ") then
          name(1,0) = a.substring(12)
     end if

     if a.contains("name(2,0) = ") then
          name(2,0) = a.substring(12)
     end if

     if a.contains("name(3,0) = ") then
          name(3,0) = a.substring(12)
     end if
next

and so on. could you please help me?
Posted

1 solution

If you don't want to do it that way, I would be tempted to use a regex:
VB
Public Dim regex As Regex = New Regex( _
      "(?<=name\()(\d+),(\d+)\)", _
    RegexOptions.IgnoreCase _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )

That captures the two numbers into numbered groups - you could then use a simple select case statement on the first group to decide what text you need.


"could you please just give me an example code for that for each statement"


VB
Private Shared findNames As New Regex("(?<=name\()(\d+),(\d+)\)", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.IgnorePatternWhitespace Or RegexOptions.Compiled)
Private Shared Sub DoWork()
	Dim inp As String = "name(0,0) = Hello" & vbLf & "name(1,0) = World" & vbLf & "name(2,0) = Something" & vbLf
	Dim matches As MatchCollection = findNames.Matches(inp)
	For Each match As Match In matches
		Select Case match.Groups(1).Value
			Case "0"
				' Do something with name(0,0)
				Console.WriteLine("ZERO")
				Exit Select
			Case "1"
				' Do something with name(1,0)
				Console.WriteLine("ONE")
				Exit Select
			Case "2"
				' Do something with name(2,0)
				Console.WriteLine("TWO")
				Exit Select
		End Select
	Next
End Sub
 
Share this answer
 
v2
Comments
Member 8378691 13-Apr-12 3:31am    
could you please just give me an example code for that for each statement
OriginalGriff 13-Apr-12 3:59am    
Answer updated
Nelek 13-Apr-12 5:04am    
Nice answer. +5
Nelek 13-Apr-12 5:04am    
If the answer helped you, please don't forget to accept it
Member 8378691 13-Apr-12 23:47pm    
it didn't help me. instead of the console writeline("ONE/TWO/THREE") part, i want to write something like:

number(200) = a.substring(6)

could you please show me how to insert it?

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