Solved

# Separating Variable From Operator

Posted on 2009-07-08
240 Views
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.
0
Question by:emi_sastra
• 2
• 2

LVL 39

Expert Comment

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)
``````
0

LVL 1

Author Comment

Hi pratima_mcs,

Is there any other simple way?

You know operator  more than "+-*"

May be using regular expression?

Thank you.

0

LVL 39

Expert Comment

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
0

LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
Maybe something like this will work for you?
``````    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim strFormula As String = "abc+cde-fgh*1"

Dim strVariable1 As New System.Text.StringBuilder

Dim strVariable2 As New System.Text.StringBuilder

For Each C As Char In strformula

If Char.IsLetterOrDigit(C) Then

strVariable1.Append(C)

Else

strVariable1.Append(",")

strVariable2.Append(IIf(strVariable2.Length = 0, C, "," & C))

End If

Next

Label1.Text = strVariable1.ToString

Label2.Text = strVariable2.ToString

End Sub
``````
0

LVL 1

Author Comment

Hi Idle_Mind,

I think your code is what I am looking for.

Thank you very much for your help.
0

## Join & Write a Comment Already a member? Login.

Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!