|
I am looking for a Tokenize function that respects (i.e. ignores) certain pairs of characters, like quotes, brackets etc.
(regarding priority and escape characters, C# syntax preferred )
i.e.
Tokenize("Hello, 'World,Worlds', something(goo,gambas)", ',')
should return an array with the following strings:
Hello
World,Worlds
something(goo,gambas)
I just wish wish wish :heart: that someone already has something...
|
|
|
|
|
Here is what you seek.
(but, in my opinion, Split method of String does it's job well. Look here.)
-------------------------
Listen up! Teamwork means staying out of my way! (Seifer, Final Fantasy 8).
|
|
|
|
|
Hey peterchen. Looks like Dmitry has pointed you to an article that may work.
In case it doesn't work, here's something I coded up. I spent a good 20 minutes or so trying 3 different solutions. This one seems to work best, although I haven't run it through any kind of rigorous testing. It uses C# 2's iterators for great performance -- it only does work when you ask for results. Here it is:
public IEnumerable<string> Tokenize(string input, char seperator)
{
int lastSeperatorIndex = 0;
int openSingleQuoteCount = 0;
int openParenthesisCount = 0;
for (int i = 0; i < input.Length; i++)
{
char character = input[i];
if (character == '(')
{
openParenthesisCount++;
}
else if (character == ')' && openParenthesisCount > 0)
{
openParenthesisCount--;
}
else if (character == '\'')
{
openSingleQuoteCount = (openSingleQuoteCount == 0) ? openSingleQuoteCount + 1 : openSingleQuoteCount - 1;
}
else if (character == seperator && openParenthesisCount == 0 && openSingleQuoteCount == 0)
{
yield return input.Substring(lastSeperatorIndex, i - lastSeperatorIndex + 1).Trim(seperator);
lastSeperatorIndex = i;
}
if (i == input.Length - 1 && i != lastSeperatorIndex)
{
yield return input.Substring(lastSeperatorIndex, i - lastSeperatorIndex + 1).Trim(seperator);
}
}
}
Putting in this string: "Hello, 'World,Worlds', something(goo,gambas), blahFoo(inner(double, inner), outer)"
Results in "Hello", "'World,Worlds'", "something(goo,gambas)", "blahFoo(inner(double, inner), outer)".
For what it's worth, I bet LINQ would let us write a little query on the string to accomplish the same thing, only in about 3 or 4 lines of code.
|
|
|
|
|
Judah Himango wrote: I bet LINQ would let us write a little query on the string to accomplish the same thing, only in about 3 or 4 lines of code.
If only we knew how these lines have to look!
Using an enumerator and yield is a good idea, I'll have to add support for double quotes and square brackets - tomorrow
P.S. I'm always amazed how far you go out of your way to help. I don't kow how to say "thank you".
|
|
|
|
|
peterchen wrote: If only we knew how these lines have to look!
I just downloaded Orcas Beta 1. Maybe I'll give it a shot with LINQ. Would be a good learning experience.
peterchen wrote: P.S. I'm always amazed how far you go out of your way to help. I don't kow how to say "thank you".
Hey, thanks man, that's nice to hear.
|
|
|
|
|
Hello!
hope you all are doing fine, following is the scenario:
- we have an executable A.exe
- we have another executable B.exe
- both are VC# Win Apps
problem:
- we execute A.exe
- when the user press Finish button, we want to do the following 2 things
1. execute B.exe
2. close A.exe
the catch is, we donot want to close B.exe!!tried it with process class, but it doesnt help!
Thanks in advance! !
|
|
|
|
|
In the finish button you could use Process.Start to run B.Exe and then call the code to close A.exe
|
|
|
|
|
Thanks, i tried that idea before but did a blunder, its working now!! !
|
|
|
|
|
(It seems to be the topic of the day )
If I have the name of a class as a string, how do I:
* Test if this class exists in the current assembly
* Figure out if this class supports a given interface IFoo
(I guess i have to go class name string --> Type --> is?)
* Create an instance (System.Type could do that again I guess)
|
|
|
|
|
you can do this by Assembly
|
|
|
|
|
Here's how I do this task in my class factories:
First use System.Type.GetType("your class name") to get a Type object for the class. You can then use the Assembly property of that Type object to get the assembly that has that class. Use the GetInterface("your interface") method to see if the class implements a given interface. If the method returns null or the type object returned is not an instance of the given interface, the class does not implement the interface. Finally, you can use Activator.CreateInstance(class type object) to get an instance of your class. I've included some example code below.
<br />
public YourInterfaceOrClassType GetClassInstance(string classNameString)<br />
{<br />
System.Type classType = System.Type.GetType(classNameString);<br />
<br />
if(classType == null)<br />
throw new TypeLoadException(String.Format("Type {0} does not exist.", classNameString));<br />
<br />
Type interfaceType = classType.GetInterface(yourInterfaceNameString, true);<br />
if(interfaceType == null || !interfaceType.Equals(typeof(YourInterfaceType)))<br />
throw new TypeLoadException(String.Format("Type {0} does not implement the {1} interface.", classNameString, yourInterfaceNameString));<br />
<br />
if(classType.IsAbstract)<br />
throw new TypeLoadException(String.Format("Type {0} is abstract and cannot be instantiated.", classNameString);<br />
<br />
return (YourInterfaceOrClassType)Activator.CreateInstance(classType);<br />
}<br />
-- modified at 18:51 Friday 20th April, 2007
|
|
|
|
|
Hey, that works! thanks!
|
|
|
|
|
Does anyone have a nice control derived from IToolboxService? Maybe like a Visual Studio 2005 style? I cannot seem to find one? I looked on CodeProject and found some toolboxes but they were NOT derive from the IToolboxService class. I know I could do this my self and it would probably take some time. I was just curious if anyone out there has found one.
Chris
|
|
|
|
|
Hello,
I need help in"Master/details form" what does it mean?
Thanks.
Dad
|
|
|
|
|
Here is a general explanation of what "Master-details form" is.
Here and here are some explanations on how to create it with VS2005.
here is the article on CodeProject related to it.
Hope it helps,
- Dmitry.
-------------------------
Listen up! Teamwork means staying out of my way! (Seifer, Final Fantasy 8).
|
|
|
|
|
Is there an IList implementaiton that fires a change notification when the list is moified (items added, removed, list cleared)?
|
|
|
|
|
I found WIntellect PowerCollections provide ListBase<t> class that makes implementing this fairly easy. (not terribly efficient, but ah well )
|
|
|
|
|
peterchen wrote: Is there an IList implementaiton that fires a change notification when the list is moified (items added, removed, list cleared)?
Off the top of my head...
Does the BindingList collection fit the bill? It implements the IBindingList interface which has an event for change notification (I also think it implements the IList interface, but don't hold me to that).
|
|
|
|
|
That looks almost like the thing I wrote now
(But it's probably more efficient)
|
|
|
|
|
Hi all,
I have a name of a windows form in string type. I would like to generate it in runtime. How can I succeed this?
ex:
//my form class is MainPage let's say.
//I have :
string nameOfPage = "MainPage";
//I am searching a way of generating a form of type MainPage by just using variable nameOfPage
Thanks a lot!
|
|
|
|
|
Create an object of the form and set the name of the object as MainPage
Form newForm = new Form();
newForm.Name = "frmMainPage";
newForm.Size = new Size(200, 200);
newForm.Location = new Point(10, 10);
newForm.Show();
Oops I read your question wrong . You must be using what Colin said
Last modified: 11mins after originally posted --
|
|
|
|
|
IMHO this is not OP asks.
Perhaps he wants to dynamically create an instance of a class whose type is given by the "frmMainPage" string. Do you agree?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
CPallini wrote: Perhaps he wants to dynamically create an instance of a class whose type is given by the "frmMainPage" string. Do you agree?
Yeah, realized I read his question wrong
|
|
|
|
|
A form is just a class. You can use reflection to instantiate an object of a class if you know the name of the class.
See the MSDN documentation in the System.Reflection namespace.
|
|
|
|
|
OK --first post...;P
I have a windows app (simulation tool) that does a bunch of background number crunching. This is my first full C# project.
I want to stream some progress messages back to the user since the full simulation takes some time to run. (a la the messages that scroll by in a box during some software installation) The messagebox doesn't really do the trick.
Right now I am using a readonly textbox on the form and adding each message to the .Text property of the box, but this does not seem like the most efficient way. It usually doesn't actually display anything until it has all the messages --you just see the scroll bar changing in an empty box and then all the text appears, and when it's all said and done, there will be alot of messages and this seems like a lot of redrawing mostly the same thing...)
Basically, I want the equivalent of Console window Writeline messaging but delivered to a windows app something. It seems like there should be such a thing, but I can’t find it.
Thanks in advance.
-- modified at 12:49 Thursday 19th April, 2007
OK --so it turns out the property I was looking for was AppendText. I just didn't find that in any convenient documentation sources. It sounds like the Application.DoEvents() has some real memory use problems
|
|
|
|
|