Solved

Splitting variable into 2 values

Posted on 2006-11-15
5
191 Views
Last Modified: 2010-04-23
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
Comment
Question by:wilcor14
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17948276
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 17948294
Hi wilcor14,

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

Cheers!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17948354
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
 

Author Comment

by:wilcor14
ID: 17948378
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
 

Author Comment

by:wilcor14
ID: 17953509
It works perfectly. Sorry for the delay. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now