String Operations
-
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