Solved

Splitting variable into 2 values

Posted on 2006-11-15
5
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

734 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