HI
this is pradeep, i m vb.net learner,my English not good dont mind
what i need is i what to get all information within the parenthesis of Sin().
for example , i have created one one textbox in my user interface.
i wanted users to enter some trigonometric expresiions like " sin(90+(25+25))"
for that i what all the information present with in the parenthesis of sin()
can anyone give the code to help me
thanks in advance

Dani AI

Generated

As suggested, you do need to parse the text rather than rely on a single simple find. your GetMid was a useful start for one occurrence, but it assumes a single sin(...) and can go out of range. The snippet below shows a robust, case‑insensitive VB.NET routine that finds every sin(...) in a string, skips optional whitespace between sin and (, avoids matching sin inside longer identifiers, and correctly handles nested parentheses.

Public Function ExtractSinArguments(input As String) As List(Of String)
    Dim results As New List(Of String)()
    Dim pos As Integer = 0
    While True
        Dim sinPos As Integer = input.IndexOf("sin", pos, StringComparison.OrdinalIgnoreCase)
        If sinPos = -1 Then Exit While
        If sinPos > 0 AndAlso (Char.IsLetterOrDigit(input(sinPos - 1)) OrElse input(sinPos - 1) = "_"c) Then
            pos = sinPos + 3
            Continue While
        End If
        Dim openPos As Integer = sinPos + 3
        While openPos < input.Length AndAlso Char.IsWhiteSpace(input(openPos))
            openPos += 1
        End While
        If openPos >= input.Length OrElse input(openPos) <> "("c Then
            pos = sinPos + 3
            Continue While
        End If
        Dim depth As Integer = 1
        Dim i As Integer = openPos + 1
        While i < input.Length AndAlso depth > 0
            If input(i) = "("c Then depth += 1 ElseIf input(i) = ")"c Then depth -= 1
            i += 1
        End While
        If depth = 0 Then
            Dim inner As String = input.Substring(openPos + 1, i - openPos - 2)
            results.Add(inner.Trim())
            pos = i
        Else
            Exit While
        End If
    End While
    Return results
End Function

Example: calling ExtractSinArguments("sin(90+(25*2))+sin(45+(25*2))") yields ["90+(25*2)","45+(25*2)"]. Notes: handle unmatched parentheses per your UI (report error or skip). If you plan to evaluate the extracted expressions numerically, remember System.Math.Sin expects radians — convert degrees with radians = degrees * Math.PI / 180 (System.Math.Sin). Avoid regex for nested parentheses; this stack/count approach is simple and O(n).

Recommended Answers

All 5 Replies

if user enters "sin(90+(25*2))+sin(45+(25*2))"
how can i get "90+(25*2)" and "45+(25*2)"
please help me.thank you for your reply

you will have to parse the line yourself I think. check out the instr, and mid$ functions. The presence of a second set of brackets is going to make it tricky. have a think about the logic and post what you come up with.

Private Function GetMid(somestring As String) As String
Dim startPos As Integer = somestring.IndexOf("sin(") + 4

Dim parenCount As Integer = 1
Dim innerLen As Integer = 0

For i As Integer = startPos To somestring.Length

If somestring(i) = "("c Then
parenCount += 1
End If

If somestring(i) = ")"c Then
parenCount -= 1
End If

If parenCount = 0 Then
Exit For
End If

innerLen += 1
Next


Return somestring.Substring(startPos, innerLen)
End Function

the above code is when textbox has only one sin() function,how to get if more than one sin() present in textbox??

How are these sins:D present in the TextBox?
1 sin per each line?:D
2 or more sins per line?:D
Or is it just a TextBox full of sins?:D with no hope of ever going to heaven.LMAOOO:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.