Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

Separating Variable From Operator

Hi,

I have a variable like :

strFormula = "abc+cde-fgh*1"

I want to separate them into 2 stringbuilder or other variable:

strVariable1 = abc,cde,fgh

strVariable2 = +,-,*

How could I do it?

Thank you.
Avatar of Pratima
Pratima
Flag of India image

try some thing like this
  Dim strFormula, strVariable1, strVariable2 As String
        strFormula = "abc+cde-fgh*1"
        strVariable1 = ""
        strVariable2 = ""
        If (strFormula.Contains("+")) Then
            strFormula = strFormula.Replace("+", ",")
            If (strVariable2 = "") Then
                strVariable2 = "+"
            Else
                strVariable2 = strVariable2 + "," + "+"
            End If
        End If
        If (strFormula.Contains("-")) Then
            strFormula = strFormula.Replace("-", ",")
            If (strVariable2 = "") Then
                strVariable2 = "-"
            Else
                strVariable2 = strVariable2 + "," + "-"
            End If
        End If
        If (strFormula.Contains("*")) Then
            strFormula = strFormula.Replace("*", ",")
            If (strVariable2 = "") Then
                strVariable2 = "*"
            Else
                strVariable2 = strVariable2 + "," + "*"
            End If
        End If
        strVariable1 = strFormula
 
        Response.Write(strVariable1)
        Response.Write(strVariable2)

Open in new window

Avatar of emi_sastra
emi_sastra

ASKER

Hi pratima_mcs,

Is there any other simple way?

You know operator  more than "+-*"

May be using regular expression?

Thank you.

yu need to add
 If (strFormula.Contains("+")) Then
            strFormula = strFormula.Replace("+", ",")
            If (strVariable2 = "") Then
                strVariable2 = "+"
            Else
                strVariable2 = strVariable2 + "," + "+"
            End If
        End If

for each operator
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Idle_Mind,

I think your code is what I am looking for.

Thank you very much for your help.