need help with c# programming (in Off-topic)


alisa [The Forgehood] June 1 2009 11:38 PM EDT

Hey, i need some help with some C# programming. I need to do some text processing.

The part I'm stuck at is that I need to search for a word in a string. It might be anywhere in the string. (it is not part of another word)
Like GPS.

Then I need to set parameters, for the program to take the word GPS and 4 lines after that, or 4 numbers separated by commas.

If anybody can help.. Will be greatly appreciated.

Example of the string:

987654321 21/12/2009 11:34:21
GPS; 1; 2; 3; 4

In this example, I use ; as the separator.
There might be other stuff behind or in front, so I would like to set definitive parameters.

This is part of an SMS program for my Final Year Project. If you need said program to figure out this part, can CM me and I will send it.

Thanks in advance!~

bartjan June 2 2009 12:08 AM EDT

Regular expressions are the tool for a job like this. It seems like even C# supports those.

With a regexp like "\bGPS\b" you're matching only the string GPS as a whole word and not if it's a part of a word.

alisa [The Forgehood] June 2 2009 12:25 AM EDT

oh.. thanks..

can regular expression set the parameters too?

cause sometimes i get GPS;1;2
sometime i get GPS only..
i need the GPS;1;2;3;4 and nothing behind or in front of that..


spell checker spell checker spell checker spell checker

QBsutekh137 June 2 2009 12:40 AM EDT

alias, strings in c# are replete with functions...type a period after one if you are using any studio with tips... I am not sure of the syntax, but I know I could parse whatever you are trying to do in Visual Foxpro, and that's a dead language. *smile*

When trying to find stuff in strings, I find it helps me (conceptually) to split the front off. In other words, use variables liberally. Get a variable that starts with the "GPS", then parse from there. Don't try to do everything all at once, split it out. Sure, other folks might get something more elegant (regexp definitely helps there), but you'll get there. The parameters you are referring to are essentially delimiter and position. I know Foxpro has an OCCURS() function that takes what you are looking for and a number of occurence. I am pretty sure C# has the same, at least in Visual Studio 2005 and beyond.

QBsutekh137 June 2 2009 12:40 AM EDT

*alisa

Sorry, I got your name wrong. *smile*

Daz June 2 2009 12:54 AM EDT

Yeah, you can use something like 'location = myString.subString("GPS");' to see if it exists. If it doesn't, location will be 0, and if it does, location should be equal to where the G is. Note, I may be getting this confused with another of the string functions. It should be in there somewhere, though. I haven't used C# in a little while, to be honest. Stupid PHP :(

alisa [The Forgehood] June 2 2009 1:04 AM EDT

hmm.. getting a little boggled here..

anybody got any examples?

y i can only have 1 string is because the stuff in that string is from an sms.. then from the sms, i need to derive the "GPS;1;2;3;4" then split it into separate string variable to update my database.....

blackshadowshade June 3 2009 7:54 AM EDT

From this site and this book, page 150, it seems that all you need to do to locate the substring is:

using System.Text.RegularExpressions;
location = myString.IndexOf("GPS");

blackshadowshade June 3 2009 8:05 AM EDT

Oh, sorry, forgot about the digits.

You can specify known string patterns as a regular expression.

For example, if you've found "GPS" alread and want to take the "; 1; 2; 3; 4" afterwards, but the numbers, puncuation and type of spacing are unknown, you could specify the regular expression as

/([[:punct:]][[:space:]][[:digit:]]){4}/

One good place to learn about regexp is http://www.gnu.org/manual/gawk/html_node/Regexp.html

Once you have the regexp, you can use it this way:
http://en.csharp-online.net/CSharp_Regular_Expression_Recipes%E2%80%94Enumerating_Matches

blackshadowshade June 3 2009 8:13 AM EDT

I've been checking a couple of regex links for C# and haven't noticed the use of character lists anywhere.

Thus, if [[:digit:]] doesn't work for some reason, then [0-9] will work instead.

Similarly, if [[:punct:]] doesn't work, you can replace it with a set of punctuation that is acceptable, for example, [;.:], or just specify directly that it has to be a semicolon.

Space characters can be specified with their backslash codes, for example [ \t] would accept spaces and tabs.
This thread is closed to new posts. However, you are welcome to reference it from a new thread; link this with the html <a href="/bboard/q-and-a-fetch-msg.tcl?msg_id=002mVz">need help with c# programming</a>