Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Splitting variable into 2 values

I have the following value in the variable sExerciseStage:

The Motor Bike Starter(stage1)

I need "The Motor Bike Starter" to be assigned to a new variable sExercise
and the # "1" to be assigned to a new variable iStage

Can anyone help me split this? Thanks

0
wilcor14
Asked:
wilcor14
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Many ways to do it...

Here's one:

        Dim sExerciseStage As String = "The Motor Bike Starter(stage1)"

        Dim parts() As String = sExerciseStage.Split("(".ToCharArray)

        Dim sExercise As String = parts(0).Trim
        Dim iStage As Integer = Integer.Parse(parts(1).Replace("stage", "").TrimEnd(")").Trim)

        Debug.WriteLine("sExercise = " & sExercise)
        Debug.WriteLine("iStage = " & iStage)
0
 
mmarinovCommented:
Hi wilcor14,

System.Text.RegularExpressions.Regex.Split("The Motor Bike Starter(stage1)", "\\(stage");

Cheers!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
wilcor14,

We have both made assumptions about the source string...

For a bulletproof solution, answer these questions:

    Will there always be a "(" after the first part?

    Will there always be a trailing ")" at the end?

    Will "stage" ALWAYS be before the number you want?

If "No" to either then please post some examples of the strings you need parsed...
0
 
wilcor14Author Commented:
Idle_Mind,

Your assumptions are correct. I'm testing it right now and I think this will work perfect. Give me a few moments.
0
 
wilcor14Author Commented:
It works perfectly. Sorry for the delay. Thanks for your help.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now