String Operations
-
i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.
-
i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.
Do you mean you've used the string.Split(string[], StringSplitOptions) and you want to know which character has caused the split? There isn't an overload that returns that information. You'll have to roll your own method, if you need it a lot you could always create it as an extension method to the string class. Remember there could be 0 to n number of characters that were used so you'll have to return an array or list of some sort.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Do you mean you've used the string.Split(string[], StringSplitOptions) and you want to know which character has caused the split? There isn't an overload that returns that information. You'll have to roll your own method, if you need it a lot you could always create it as an extension method to the string class. Remember there could be 0 to n number of characters that were used so you'll have to return an array or list of some sort.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)can you help me out to create that own function..
-
can you help me out to create that own function..
Your best bet is to examine the particular overload you want in the .NET source[^] and recreate that method with the following alterations. Create and initialize a List<string> field (eisier than an array as it's dynamic). Everytime a split occurs, check if the list contains that character and if not add it. Give the method a string[] out parameter so you can return the extra data. At the end of the method before you return, call the list's ToArray method and pass the value to the out.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.
Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!
string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
:)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!
string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
:)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Very cool Luc :cool:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!
string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
:)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Its Looking Really cool,,thanks for Help but one thing more i have have string array collection, not char array collection,so how can i do this with string array collection..
-
Its Looking Really cool,,thanks for Help but one thing more i have have string array collection, not char array collection,so how can i do this with string array collection..
if you want the result to be a char array, you have to turn my result into a char array; I have already shown how that is done. It is just more of the same stuff. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!
string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
:)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Hi, it is not very clear. if you want to split a string s1 using a collection of split characters (say all chars in string s2) and are interested in which split characters actually occur in s1, then this strange snippet offers exactly that; I leave it to the reader to discover how it works!
string[] sa=s1.Split(string.Join("", s1.Split(s2.ToCharArray())).ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
:)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
I'm glad I don't have to maintain that piece of code. Curious to see what a LINQ-based solution would read like.
Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I'm glad I don't have to maintain that piece of code. Curious to see what a LINQ-based solution would read like.
Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
The good thing is it doesn't need any maintenance :laugh: ; it could be implemented as an extension method to the string class, I would suggest it gets named RemoveNot(s1,s2): remove all chars from s1 that are not in s2. No idea what LINQ can do here. And also no idea what regex could do (I was surprised no one ever mentioned it so far). I am pretty sure both of them would look even worse, and have lower performance. A lambda expression might come in handy, haven't really considered it (am still working in .NET 2.0) :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
The good thing is it doesn't need any maintenance :laugh: ; it could be implemented as an extension method to the string class, I would suggest it gets named RemoveNot(s1,s2): remove all chars from s1 that are not in s2. No idea what LINQ can do here. And also no idea what regex could do (I was surprised no one ever mentioned it so far). I am pretty sure both of them would look even worse, and have lower performance. A lambda expression might come in handy, haven't really considered it (am still working in .NET 2.0) :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.
This code will split the string, and give you the result as two arrays; one array with the regular split result, and one array with the splitters that was used. The
usedSplitters
array is one item shorter than thewords
array. If no splitters were found, thewords
array contain the original string, and theusedSplitters
array is empty.string source = "asdf,asdf;;;asdf,asdf::asdf..asdf";
string[] splitters = { "..", ",", ";;;", "::" };string pattern = "(" + string.Join("|", splitters.Select(s => Regex.Escape(s)).ToArray()) + ")";
MatchCollection matches = Regex.Matches(source, pattern);
string[] words = new string[matches.Count + 1];
string[] usedSplitters = new string[matches.Count];
int pos = 0;
for (int i = 0; i < matches.Count; i++) {
words[i] = source.Substring(pos, matches[i].Index - pos);
usedSplitters[i] = matches[i].Value;
pos = matches[i].Index + matches[i].Length;
}
words[matches.Count] = source.Substring(pos);Despite everything, the person most likely to be fooling you next is yourself.
-
Luc Pattyn wrote:
And also no idea what regex could do (I was surprised no one ever mentioned it so far).
Getting there, just have to read the replies so far first... :)
Despite everything, the person most likely to be fooling you next is yourself.
I'll hold the line
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
This code will split the string, and give you the result as two arrays; one array with the regular split result, and one array with the splitters that was used. The
usedSplitters
array is one item shorter than thewords
array. If no splitters were found, thewords
array contain the original string, and theusedSplitters
array is empty.string source = "asdf,asdf;;;asdf,asdf::asdf..asdf";
string[] splitters = { "..", ",", ";;;", "::" };string pattern = "(" + string.Join("|", splitters.Select(s => Regex.Escape(s)).ToArray()) + ")";
MatchCollection matches = Regex.Matches(source, pattern);
string[] words = new string[matches.Count + 1];
string[] usedSplitters = new string[matches.Count];
int pos = 0;
for (int i = 0; i < matches.Count; i++) {
words[i] = source.Substring(pos, matches[i].Index - pos);
usedSplitters[i] = matches[i].Value;
pos = matches[i].Index + matches[i].Length;
}
words[matches.Count] = source.Substring(pos);Despite everything, the person most likely to be fooling you next is yourself.
I like the concept, my Visual Studio targetting 3.5 does not know the Select method though. Anyway, it is not one of those obscure regex applications... :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
I like the concept, my Visual Studio targetting 3.5 does not know the Select method though. Anyway, it is not one of those obscure regex applications... :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
I think that you need
using System.Xml.Linq;
to get the Select extension. Here's a way to create the pattern without it:StringBuilder builder = new StringBuilder().Append('(');
bool first = true;
foreach (string s in splitters) {
if (first) {
first = false;
} else {
builder.Append('|');
}
builder.Append(Regex.Escape(s));
}
string pattern = builder.Append(')').ToString();Despite everything, the person most likely to be fooling you next is yourself.
-
I think that you need
using System.Xml.Linq;
to get the Select extension. Here's a way to create the pattern without it:StringBuilder builder = new StringBuilder().Append('(');
bool first = true;
foreach (string s in splitters) {
if (first) {
first = false;
} else {
builder.Append('|');
}
builder.Append(Regex.Escape(s));
}
string pattern = builder.Append(')').ToString();Despite everything, the person most likely to be fooling you next is yourself.
the original works like a charm once I added
using System.Linq;
Without Select, I would writeStringBuilder builder = new StringBuilder().Append('(');
string sep="";
foreach (string s in splitters) {
builder.Append(sep).Append(Regex.Escape(s));
sep="|";
}
string pattern = builder.Append(')').ToString();avoiding the extra state variable and if-else test. Thanks. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
the original works like a charm once I added
using System.Linq;
Without Select, I would writeStringBuilder builder = new StringBuilder().Append('(');
string sep="";
foreach (string s in splitters) {
builder.Append(sep).Append(Regex.Escape(s));
sep="|";
}
string pattern = builder.Append(')').ToString();avoiding the extra state variable and if-else test. Thanks. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Is it better? You are just replacing one state variable with another, making the value of the variable double as both state and value. :)
Despite everything, the person most likely to be fooling you next is yourself.
I trust this is subjective, however I see lots of small reasons, yes: I find it shorter, more readable, less error prone, and possibly better for instruction scheduling. Also for testing and proofing correctness avoiding if-tests generally is beneficial. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
i Have passed an string array into string.split function,the target string splits successfully,but i need to get string array information on which string has been split.
If you mean that you want to get the string that was originally split from the array, you will either have to write a method to do that, or you can use my handy-dandy string parsing class, available here[^] on CodeProject.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001