473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

.net 2.0 split and knowing which seperator was used to split the string

following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?

string[] charSeparators = { "=", ">=", "<=" , "!=" };

string s1 = "field != value"

result = s1.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
Jan 16 '06 #1
2 2165
DF,

No, there is no way with that call to know which separator was used.

What you could do is make individual calls to split, using one delimiter
at a time, and then determine if a split was used based on the number of
elements returned.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:pu********************************@4ax.com...
following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?

string[] charSeparators = { "=", ">=", "<=" , "!=" };

string s1 = "field != value"

result = s1.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);

Jan 16 '06 #2
The short answer is, all of them are used. When you pass an array of strings
to the String.Splite method, all of the strings in the array are used. If,
for example, you had a string like the following:

"a != b && b <= c"

The string would be split into 3 substrings.

So, first, there is a flaw in your reasoning. You're apparently assuming
that the string to split will have one and only one of the substrings in the
array in it. This may be true, as you created the business rules, and wrote
the code, but neither I nor the String.Split method can know this.

Now, if you were to inform me that you are working with a business rule that
enforces this, there are several alternatives available to you, all of which
use the String.IndexOf method, and some of which may employ the String.Split
method, but not necessarily by any means. In fact, the String.Split method
would not be employed in the best of these solutions.

To break it down for you, first, you need to identify which one (and only
one) of the strings in the array is in the string to split. This involves
looping through the array. Example:

string testString = "a != b";
int i, index;
string start, end;
string[] charSeparators = { "=", ">=", "<=" , "!=" };
for (i = 0; i < charSeparators.Length; i++)
{
if ((index = testString.IndexOf(charSeparators[i])) > -1)
{
}
}

When the statement evaluates to true, you've found the separator. However,
you don't need String.Split at this point. You can split the string with the
substring and the index:

if ((index = testString.IndexOf(charSeparators[i])) > -1)
{
start = testString.Substring(0, index);
end = testString.SubString(index + charSeparators[i].Length);
break;
}

At this point, "i" is the index in charSeparators of the substring found.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:pu********************************@4ax.com...
following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?

string[] charSeparators = { "=", ">=", "<=" , "!=" };

string s1 = "field != value"

result = s1.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);

Jan 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
5
by: Arjen | last post by:
Hi All, What I want to is using a string as PATTERN in a split function. This makes it possible for me to change the PATTERN on one place in my script... For example: $separator = ";"; $line...
2
by: Teis Draiby | last post by:
Does this already exist? Something like "\£" that would appear like a "," or "." dependent on the current locale settings. Examples: string stringValue = "1000\£54"...
4
by: Jesper Denmark | last post by:
Hi, I use the double.Parse functionality to convert a number in a text file into a double. However, while this works fine on one computer it doesn't on another. I've found out that it is...
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
1
by: randomtalk | last post by:
hello everyone! I can't seem to find a function that combines a list of items into a string with a seperator between the individual elements.. Is there such a method that does the opposite of...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
3
by: Ondrej Baudys | last post by:
Hi, After trawling through the archives for a simple quote aware split implementation (ie string.split-alike that only splits outside of matching quote) and coming up short, I implemented a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.