473,756 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.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(charSe parators,
StringSplitOpti ons.RemoveEmpty Entries);
Jan 16 '06 #1
2 2194
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.co m

"Digital Fart" <pr*********@an gelfire.com> wrote in message
news:pu******** *************** *********@4ax.c om...
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(charSe parators,
StringSplitOpti ons.RemoveEmpty Entries);

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.Inde xOf(charSeparat ors[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.Inde xOf(charSeparat ors[i])) > -1)
{
start = testString.Subs tring(0, index);
end = testString.SubS tring(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*********@an gelfire.com> wrote in message
news:pu******** *************** *********@4ax.c om...
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(charSe parators,
StringSplitOpti ons.RemoveEmpty Entries);

Jan 16 '06 #3

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

Similar topics

11
2506
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 strings. I wonder if someone else had ever stumbled on it before, and if it has a good reason to work like it is. Both implementations take two parameters: the separator character and the max number of splits (maxsplit). However, string.split() accept
5
3271
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 list. assume the string is in the variable f, then i tried f.split() but i end up with
5
7565
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 = "field1;value1"; local($field, $value) = split(/$separator/, $line);
2
1649
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" Console.WriteLine(stringValue); // Outputs "1000,54" or "1000.54" dependent on locale settings:
4
12225
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 dependent on the comma seperator settings on the computer. Is there a way to override this - I would like double.Parse to always use a . as comma seperator no matter what the system settings are.
4
728
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 ignore pairs of brackets like: "one" <tab> "two three" ( four "five six" ) Want fields like:
1
22422
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 sting.split? thanks alot!
1
8375
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 you can see, there could have been a problem with the split-method. The following short article handles ways around this possible problem, that we couldn't reproduce, but someone may possibly encounter it too sometimes. If nothing else, the shown...
3
1764
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 quick and dirty function that suits my purposes. It's ugly and it doesn't use a stack, it only supports a single character as a 'sep' function, only supports one type of quote (ie ' or " but not both), but it does the job, and since there have...
0
9462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.