|
hi everyone!
i would like to do the following task:
i do have a string and want to divide it into parts
e.g. "test me for more tests to get the test results after having completed the test task" should be divided up into
"test me for more"
"tests to get the"
"test results after having completed the"
"test task"
The string.split() functions splits by characters, but i want to split a string by word or even a whole line without having to parse the string line by line to divide it up.
e.g.
"test me for more tests to get the test results after having completed the test task
next one: test me for more tests to get the test results after having completed the test task
and again: test me for more tests to get the test results after having completed the test task"
should be divided up (by "test me") into
"test me for more tests to get the test results after having completed the test task next one:"
"test me for more tests to get the test results after having completed the test task and again:"
"test me for more tests to get the test results after having completed the test task"
does anyone of you know a good way how to do this?
so some kind of tokenizing like in C and C++ would be interesing. but is there something similar in C#? because in C and C++ you are able to divide strings by a token into substrings.
thanks.
stephan.
|
|
|
|
|
string.split also lets you give it a string array parameter, and it will split it everywhere it finds the string(s).
example:
<br />
string x = "test: this string should be split on every occurance of the word test!";<br />
string[] xSplit = x.Split(new string[] { "test" }, StringSplitOptions.None);<br />
<br />
for (int i = 0; i < xSplit.Length; i++)<br />
{<br />
xSplit[i] = "test" + xSplit[i];<br />
}<br />
after x.Split, xSplit will have the following strings:
[0] - ": this string should be split on every occurance of the word "
[1] - "!"
because the delimiting string gets removed.
After the for loop, it will have:
[0] - "test: this string should be split on every occurance of the word "
[1] - "test!"
which I think is what you are looking to do!
|
|
|
|
|
one correction...if the string you are splitting starts with the delimiter, then remove it before doing the split...otherwise it won't work right in that case.
<br />
if (x.StartsWith("test"))<br />
x.Remove(0, "test".Length);<br />
stick that just before the line with x.Split and you should be good.
|
|
|
|
|
mmGoDLiKe wrote: otherwise it won't work right in that case
Huh? In what way?
|
|
|
|
|
If the split string starts with the delimiter, the first resulting string will be "" and the second will be everything after the delimiter. By removing that first delimiter before you split, it will avoid the initial empty string, and it will throw the delimiter back onto the first resulting string in the for loop anyway.
Arg...you need to set a flag that determines whether or not to attach the delimiter to the first string...because in the code I showed you, it always does (which might not be good if the original string didn't start with the delimiter). You can either play with it yourself or I'll post a revision in an hour or so when I have time.
|
|
|
|
|
mmGoDLiKe wrote: the first resulting string will be ""
Yes, as it should be.
mmGoDLiKe wrote: the second will be everything after the delimiter
That makes no sense and it's not what I find when I try it.
|
|
|
|
|
If you split "test splitting this line"
you will get:
[0] ""
[1] " splitting this line"
and after re-adding "test" to each one in the for loop, you will get:
[0] "test"
[1] "test splitting this line"
which clearly isn't desirable.
Here is a quick and easy function I wrote that will work better then removing and then re-adding the delimiter:
public string[] Split(string source, string delimiter)<br />
{<br />
if (source.Length <= 1)<br />
return new string[] { source };<br />
<br />
int currentPosition = 0;<br />
<br />
List<string> splitStrings = new List<string>();<br />
<br />
while (true)<br />
{<br />
int nextDelimiterPos = (source.Length == currentPosition + 1) ? -1 : source.IndexOf(delimiter, currentPosition + 1);<br />
<br />
if (nextDelimiterPos == -1)<br />
{<br />
splitStrings.Add(source.Substring(currentPosition).Trim());<br />
break;<br />
}<br />
else<br />
{<br />
splitStrings.Add(source.Substring(currentPosition, nextDelimiterPos - currentPosition).Trim());<br />
<br />
currentPosition = nextDelimiterPos;<br />
}<br />
}<br />
<br />
return splitStrings.ToArray();<br />
}
test string: "testing the ability of this function to split this test string containing test"
delimiter: "test"
result:
[0] "testing the ability of this function to split this"
[1] "test string containing"
[2] "test"
|
|
|
|
|
|
Hi,
To split string with some fixed length ex 50,
but you don't want to split the last word.
So use
string str="<some text="">";
string destString=string.empty;
While(str.Lenght> 0)
{
if(str.Length <= 50)
destString+=str;
else
{
int index= str.SubString(0,50).LastIndexOf(" ");
index=(index>0)?index:50;
destString+=str.SubString(0,index);
str=str.SubString(index+1);
}
}
Best Regards,
Chetan Patel
|
|
|
|
|
Won't string.Split(Environment.NewLine) do what you want ?
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
You don't want Split at all because it removes the delimiter from the results and you'd need to add it back on.
You'll want to write your own version of Split that doesn't remove the delimiter.
Or wait for Microsoft to add another value to System.StringSplitOptions that causes Split to work the way you want.
|
|
|
|
|
Hi all,
I've come across an interesting problem that I'm not really sure how to get around.
I have a custom user control I designed to edit certain properties of a document, similar to the VS2005 properties area. Sometimes input needs to be validated before the user leaves a textbox, so what I did was I added an event handler to the Validate event on the individual items.
Here is my problem: the Validate event doesn't fire if you click on anything in the GUI that has "CanFocus=false." So, none of the menu strips, toolstrips, etc. cause the Validate event to fire. Neither does the Leave event or the LostFocus event.
I want it to behave the same way as the VS2005 properties window - if you are in the middle of editing a property and you click on a toolstrip button or a menustrip item, it will cancel the click and give you a validation error message. Is there any way to do this besides, say, adding a Click event handler to every control in the GUI and telling it to unfocus the property being edited?
Thanks in advance!
|
|
|
|
|
Hi there,
can a service written using C# to open(run) another application written using C# too,
In other word I wrote a service which I need to open forms and do more , so I had a windows application to perform all what all I want, but I do not know how can I call it from the service !
I hope I made my self clear and described the problem well ,
please urgent help is needed
Thanks
There is always something to learn
|
|
|
|
|
You won't see any interface if you have a service open an application. Services run on system accounts, not on user accounts, so the user would never see the open application.
If you want to control the service from a form, you can create a seperate application that configures a settings file and then restarts the service when you are finished.
If you need the application and the service to communicate in real time and relay information, then you will need to use .NET remoting or TCP for communication, or some other form of exchanging data between apps.
|
|
|
|
|
I am not sure it may be helpful for you or not. But from windows services we can open process like "notepad", write on files etc. but not fully interact with desktop.
|
|
|
|
|
A service is unlikely to be the right tool.
What is the goal?
|
|
|
|
|
the goal is that : I need the service to update a Data Base or read from it before the user log on his windows account , and then when he log on the service opens an application for him,
if the service is unlikely to be the right tool , what else can be ?
is there a way to run an application before the user log on windows, then it continues run ?
There is always something to learn
|
|
|
|
|
Just make it two seperate parts...a service that updates the database, and an application that has a shortcut in the "startup" folder so it starts automatically on login. If you need to, you can have the application notify the service when it closes, that way it will know know the user logs off as well. But, like I said, you can't have the service open an application directly, because it is running on the "system" account.
|
|
|
|
|
A Service would be running continuously whether a user is logged in or not, is that what you want?
You may want to use a Scheduled Task that executes when the user logs in.
To have an app run when the user logs in, use the Startup folder, but first be sure the user wants it to.
|
|
|
|
|
hii,
Im developing an client server application in C#.
In that i have to use Mobile Agent technology.
On the Server side there is a class...which i want send to the client...by System.IO class..let the class be Mobile...and it has a methode mobilecode()
public class Mobile()
{
public void mobilecode()
{
Code for gathering data from client.
}
}
On the Client side i read this file by stream reader...and i save this class in the same directory of the client.cs project..
on main function of client.cs
i call the class by...
Mobile.mobilecode();
but while compiling..it gives the error...
Mobile doesnt exist...
even if i make an empty class Mobile...thinking it could b overwritten by the code sent from server...dat does not happen...on runtime it reads the same old file...and not the does not updates till the code is executed.
So can anyone help me ou here...as to how to send a class file 4m server to client at runtime and access it immediately at runtime...!!
any help wud b appreciated...
Thnx in advance,,,,!!
|
|
|
|
|
You can't do that - the compiled program is exactly that - compiled. You can't just copy a .cs file beside the .exe file and expect it to read the .cs file.
Client-Server communication is usually handled in a completely different manner then what you are trying to do. You can look at this for a starting point:
http://www.codeproject.com/KB/IP/BasicTcpServer.aspx[^]
Exchanging actual class source code between client and server applications would require using dynamic compilation and reflection, which is probably way beyond the scope of what you are trying to do.
|
|
|
|
|
hey..
thx 4 d info..
thn is it possible to implement mobile agents in C#?..if yes how?
|
|
|
|
|
That depends on the complexity of the mobile agent you are creating.
If you need it to execute arbitrary code, then you will need to use dynamic compilation/reflection.
If you can replicate the entire program at each station and you only need to move its current state, then just copy over a configuration file instead of a .cs file, which allow the next station to load itself into the previous stations state and continue.
|
|
|
|
|
Hi my names Joe,
Im a newbie and I need to find out the best way to let a user be able to retrieve a list of files from a database, then obviously add a new path to a file.
The files are pdf, i need to know which control would be best to use to retrieve info from a database and then be able to edit and brwose to a directory and select a new file to be added. this info in turn will be terieved by our intranet which will display the pdf.
Donno if anyone might want to help.
Thanks
J
|
|
|
|
|
KornJoe wrote: then obviously add a new path to a file.
Not sure what this means ? Add a path to what file ?
KornJoe wrote: i need to know which control would be best to use to retrieve info from a database
No control should be used to do this, you should write code to get data from your database.
KornJoe wrote: then be able to edit and brwose to a directory
If you want a list of items to edit, a datagrid or grid view is fine. IF you want to browse for folders, you can use a FolderBrowseDialog to do this, and get back the folder path.
KornJoe wrote: this info in turn will be terieved by our intranet which will display the pdf.
If you want to change the contents of PDFs, you will need to use a third party library. No PDF support is built in.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|