Click here to Skip to main content
15,881,744 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have following code.

C#
string Test="abc * (xyz+ pqr) - 10/100";

            char[] delimiters = new char[] { '+', '-', '*', '/', '(', ')' };
            string[] parts = Test.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < parts.Length; i++)
            {
                Response.Write(parts[i]);
            }

I m getting output as abc xyz pqr 10100
But i want
abc
*
(
xyz
+
pqr
and so on.

(in c# or in javascript)
please help
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jan-13 17:48pm    
Not really a question. You got what you coded. Parsing is not just splitting. Read (yawn...) theory of parsers, see how they are implemented, etc.
I would not do it at all. Javascript can parse its own code without your help.
—SA

C#
string Test = "abc * (xyz+ pqr) - 10/100";

 char[] delimiters = new char[] { '+', '-', '*', '/', '(', ')' };

int CurrentPos = 0;

 foreach (var Elt in Test.Split(delimiters))
  {
   if (Elt != "")
   {
    Console.WriteLine("<{0}>",Elt);
    CurrentPos += Elt.Length;
   }
                
   if (CurrentPos < Test.Length)
    Console.WriteLine("<{0}>",Test[CurrentPos]);
               
   CurrentPos++;
  }
 
Share this answer
 
v2
You could iterate the string chars and parse them thusly:

C#
C#
char[] delimiters = new char[] { '+', '-', '*', '/', '(', ')' };
string eq = "abc * (xyz+ pqr) - 10/100";
StringBuilder sb = new StringBuilder();
string chunk = string.Empty;
foreach (char c in eq)
{
     if (delimiters.Contains(c))
     {
           sb.AppendLine(chunk);
           sb.AppendLine(c.ToString());
           chunk = string.Empty;
     }
     else
          chunk += c;
}
sb.AppendLine(chunk);
string result = sb.ToString();


JavaScript
C#
var delimiters = "+-*/()";
var eq = "abc * (xyz+ pqr) - 10/100";
var result = "";
var chunk = "";
        
for (var i = 0; i < eq.length; i++) 
{
     if (delimiters.indexOf(eq.charAt(i)) > -1) 
     {
          result += chunk + "<br/>";
          result += eq.charAt(i) + "<br/>";
          chunk = "";
     }
     else
          chunk += eq.charAt(i);
}
result += chunk;
document.write(result);
 
Share this answer
 
v4
To build on David_Wimbley answer, really all he missed is the call to Server.HtmlEncode(TestString)

C#
string Test = "abc * (xyz+ pqr) - 10/100";
 
char[] delimiters = new char[] { '+', '-', '*', '/', '(', ')' };
string[] parts = Test.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
     Response.Write(Server.HtmlEncode(parts[i]) + "<br />");
}
 
Share this answer
 
Comments
David_Wimbley 14-Jan-13 15:58pm    
nice catch, my 5
Add the "<br />" into your response.writeline call.

I ran it real quick and it prints out vertically not horizontally like you requested.

C#
string Test = "abc * (xyz+ pqr) - 10/100";

char[] delimiters = new char[] { '+', '-', '*', '/', '(', ')' };
string[] parts = Test.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
     Response.Write(parts[i] + "<br/>");
}
 
Share this answer
 
v2
Comments
Member 9581488 14-Jan-13 11:53am    
Its still eliminates maths signs and I dont want it.
I want each value separated.
David_Wimbley 14-Jan-13 11:55am    
ah sorry i didnt read your question clearly enough, let me see if i can throw something together.
Adam R Harris 14-Jan-13 15:54pm    
4* - you just overlooked the call to Server.HtmlEncode to encode the values. see my answer
Please see my comment to the question. I would advice totally alternative way. To understand what I mean, please consider using JavaScript eval function: http://www.w3schools.com/jsref/jsref_eval.asp[^].

Are you getting the idea? Chances are, this is all you need.

For an example of its use, please see source code of this page: http://sakryukov.org/freeware/calculator/[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900