473,231 Members | 1,687 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,231 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 2157
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.