Hey,

I am using a messenging system to do math, and I am in need of help for my string splitting.

What I need it to do, is basically see if there is a '/' '*' '+' or '-' and split it from that charectar, and ALSO split it in front of that charectar.

For example, we have an equation of 5*2.

I need it to see the '*', and split it so 5 is a string, '*' is a string, and 2 is another string using an array.

Right now I have this :

``string[] substrings = msg.Split(new Char [] {' ', '/', '*', '+', '-'});``

but it does not work. If I inputted 5*2 into that code, it gives me this array : 5 , 2.

Thanks before hand !

4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by farooqaaa

Well.. I think it will work this way:

``````string newString = textBox1.Text;

char[] separators = new char[] { ' ', '/', '*', '+', '-' };

// This adds a "<" before & after each separator
for (int i = 0; i < separators.Length; i++)
{
string str = separators[i].ToString();
newString = newString.Replace(str, "<" + str + "<");
}

// Here we use "<" to split the strings

string[] substrings = newString.Split(new Char[] { '<' });

foreach (String str in substrings)
MessageBox.Show(str);
}``````

The first variable makes a copy of textBox1.Text. The 2nd string array has all the separators. After that the for loop adds "<" before & after each separator in newString. So that we can use "<" to split the strings.

Hope that helps.

Thanks

``````string delimStr = " +-*/";
char [] delimiter = delimStr.ToCharArray();
string words = "one two+three-four*five/six";
string [] split = null;

Console.WriteLine("The delimiters are -{0}-", delimStr);
for (int x = 1; x <= 10; x++) {
split = words.Split(delimiter, x);
Console.WriteLine("\ncount = {0,2} ..............", x);
foreach (string s in split) {
Console.WriteLine("-{0}-", s);
}
}``````

Thanks everyone, but thank you especially farooqaaa, I'm now using your method and it works perfectly!

But, as I just figured out when my friend typed it in, using this method it is impossible to do equations with negative numbers.

For example, if you do a normal equation it will give this

5*2 = 5<*<2 and then splits to 5, *, 2.

But when you do negatives, it outputs

-5*2 = <-<5<*<2 and then splits to -, 5, * , 2.

How could I write this out so it will assign it to a negative value instead of an operator?

Thank you!

Okay.. This will work perfectly for negative values:

``````private int isNumber(string s)
{
int result;
int.TryParse(s, out result);

return result;
}

string newString = "-225*-2/-3/4*2222-32";

char[] separators = new char[] { ' ', '/', '*', '+', '-' };

for (int i = 0; i < separators.Length; i++)
{
string str = separators[i].ToString();

if (i == 0)
newString = newString.Replace(str, str + "<");
else
newString = newString.Replace(str, "<" + str + "<");
}

string[] substrings = newString.Split(new Char[] { '<' }, StringSplitOptions.RemoveEmptyEntries);
List<string> substrs = substrings.ToList<string>();

///////////////////////////////////////
// Start Negative Check

if (substrs[0] == "-")
{
substrs[0] = substrs[0] + substrs[1];
substrs.RemoveAt(1);
}

for (int i = 0; i < substrs.Count; i++)
{
string str = substrs[i];

if (i > 0 && isNumber(str) == 0 && (str != "-" || str != "+") && substrs[i + 1] == "-")
{
substrs[i + 1] = substrs[i + 1] + substrs[i + 2];
substrs.RemoveAt(i + 2);
}
}

// End Negative Check
/////////////////////////////////////////

foreach (String str in substrs)
MessageBox.Show(str);``````

Thanks

Edited by farooqaaa: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.