Solved

Separating Variable From Operator

Posted on 2009-07-08
5
274 Views
Last Modified: 2012-05-07
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
Comment
Question by:emi_sastra
[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 39

Expert Comment

by:Pratima Pharande
ID: 24801815
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

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24802196
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

by:Pratima Pharande
ID: 24802235
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 86

Accepted Solution

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

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24815149
Hi Idle_Mind,

I think your code is what I am looking for.

Thank you very much for your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Ctime to date time in textfile? 7 80
VB.NET Textbox input validation 4 74
Filtering a datagrid view 8 42
Create a button class for use in all vb.net apps 5 40
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

736 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