Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Javascript
I have following code.
 
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 14-Jan-13 5:02am
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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++;
  }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You could iterate the string chars and parse them thusly:
 
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
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);
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

To build on David_Wimbley answer, really all he missed is the call to Server.HtmlEncode(TestString)
 
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]) + "
");
}
  Permalink  
Comments
David_Wimbley at 14-Jan-13 15:58pm
   
nice catch, my 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add the "<br />" into your response.writeline call.
 
I ran it real quick and it prints out vertically not horizontally like you requested.
 
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/>");
}
  Permalink  
v2
Comments
Member 9581488 at 14-Jan-13 11:53am
   
Its still eliminates maths signs and I dont want it.
I want each value separated.
David_Wimbley at 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 at 14-Jan-13 15:54pm
   
4* - you just overlooked the call to Server.HtmlEncode to encode the values. see my answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 CPallini 490
2 George Jonsson 297
3 OriginalGriff 250
4 Suvabrata Roy 209
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100