robinjam

Amoungst other projects, I'm making a graph drawer that draws a graph from an equation. My question is this:

How do I make a function that looks through a string, and if it finds X with a coefficient it replaces it with "*X"

Example:

X^3-2X+5 should be convered to:

X^3-2*X+5

Any ideas

### Re: Visual Basic General Formula Formatting

Bruno Yu - MSFT

robinjam,

It is obvious that you only want to change the form shown of the statement. It means that in VB, the Console.WriteLine method contents will be changed by a function. Please write a function called SubtitudeString, using the foreach loop in it in order to check each of the X and change the output to *X.

### Re: Visual Basic General Formula Formatting

robinjam

If I loop through and replace each X with *X, then

3+X

will be replaced with

3+*X

which makes no mathematical sense whatsoever.

Any other ideas

### Re: Visual Basic General Formula Formatting

DMan1

There is a J# class that can be used for string evaluations....

otherwise here is a simple parser for your requirment:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

Dim Test As String = "(3X+21)+4X*(X^5X)"

MessageBox.Show(ParseString(Test, "X"))

End Sub

Private Function ParseString(ByVal TheString As String, ByVal TheVariable As String) As String

Dim TheReturnString As String = String.Empty

For x As Integer = 0 To TheString.Length - 1

If x <> TheString.Length - 1 Then

If TheString(x + 1) = TheVariable And IsNumeric(TheString(x)) Then

TheReturnString = TheReturnString & TheString(x) & "*"

Else

TheReturnString = TheReturnString & TheString(x)

End If

Else

TheReturnString = TheReturnString & TheString(x)

End If

Next

Return TheReturnString

End Function

### Re: Visual Basic General Formula Formatting

ReneeC

I'm sort of curious how this parser is supposed to know whether

day

is equal to a single variable name or d*a*y

### Re: Visual Basic General Formula Formatting

DMan1 ReneeC wrote:
 I'm sort of curious how this parser is supposed to know whether day is equal to a single variable name or d*a*y

Very good observation Renee...the answer is... it doesn't! (it makes an assumption of a single character string for the variable...)..however it can easily be fixed by passing a character instead of a string for the variable ### Re: Visual Basic General Formula Formatting

robinjam

This is irrelevant for my requirements. All my program needs to do is change instances of X with a coefficient(or however you spell it).