C# strings.

Started by
7 comments, last by Ksingh30 18 years ago
ok whats the best way to decompose and split strings in C#. I've been using Substring, but ive been having problems witht that. for exmple this string "((#,R,2))((#,R,3)(#,R,4))((#,R,5))" I want to decompose that into 3 strings "(#,R,2)" "(#,R,3)(#,R,4)" "(#,R,5))" what im doing is checking for the double left parent and then the double right for the double left I place a marker there called lm, and a marker for the double right paren. rm. and then using the substring(lm, rm-lm). the problem im having is it splits the 3 strings into 3 but like this "(#,R,2)" "(#,R,3)" "(#,R,5))" rather then "(#,R,2)" "(#,R,3)(#,R,4)" "(#,R,5))" so my question is is there a better way to split strings? Thanks
Advertisement
The System.Text.RegularExpression .NET Library likely will provide more control to your matching.
Recently I've had to do something similar to this in a different language, but here's the C# version of what I propose:
using System;using System.Collections;using System.Text;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            ArrayList tokens = new ArrayList();            StringBuilder strBuild = new StringBuilder();            String strInput = "((#,R,2))((#,R,3)(#,R,4))((#,R,5))";            // basically a state stack using an integral value            int parenCount = 0;            for (int x = 0; x < strInput.Length; x++)            {                char current = strInput[x];                // Save this character                strBuild.Append(current);                if(current == '(')                {                    // One more parenthesis                    parenCount++;                }                else if (current == ')')                {                    // One less parenthesis                    parenCount--;                    if (parenCount == 0)                    {                        // Save the current text                        tokens.Add(strBuild.ToString());                        // Remove the current text                        strBuild.Remove(0, strBuild.Length);                    }                }            }            // Show all of the tokens            for (int x = 0; x < tokens.Count; x++)            {                System.Console.WriteLine(tokens[x]);            }        }    }}


It's a little more work than using other functions, but it works pretty good as long as you know your input is valid, i.e. the parenthesis properly match. You will also need to add in the extra code to reduce ((Text)) to (Text), but I think you can handle that. Not that much other to explain really, but if you need any I'd be glad to though. I use this concept in parsing source code files to generate information about them (such as what the comment are, strings used, etc...) Pretty solid method [smile] Regular Expressions are a great tool, but if you are like me and don't fully understand them on an advanced enough level to get done what you want to, then you'd want to use an alternative. (Like I found a reg. exp to select all text after a // comment, but I cannot get it so it will not select it if it falls in a string, for example) Anyways, good luck!
sweet. Thank you so much. :)
Regular expressions (once you understand them) are your friend.

using System.Text.RegularExpressions;Regex rx = new Regex(@"\((\(.*?\))\)");String sTest = "((#,R,2))((#,R,3)(#,R,4))((#,R,5))";MatchCollection mc = rx.Matches(sTest);String[] aMatches = new string[mc.Count];			// populate the arrayfor(int nMatch=0; nMatch < mc.Count; nMatch++){	aMatches[nMatch] = mc[nMatch].Groups[1].Value;}// aMatches[0] = (#,R,2)// aMatches[1] = (#,R,3)(#,R,4)// aMatches[2] = (#,R,5)
wow that works even better thanks man.but can you explain this part of the code


(@"\((\(.*?\))\)");

also how would I be able to just change just 1 character out of a string.

for example string test= "abcdef"; and i wanna replace the b with a Z.
I tried doing test[1]='Z'; but I get this error
"error CS0200: Property or indexer 'string.this[int]' cannot be assigned to -- it is read only"
Quote:Original post by pragma Fury
Regular expressions (once you understand them) are your friend.

*** Source Snippet Removed ***


That's awesome! As soon as I have time to learn Regular Expressions... [wink]
Quote:
@"\((\(.*?\))\)"




@ - C# pre-fix character which means "Treat the following as a literal, do not do \escapes". Thus the \( are passed directly to the Regular Expression, which then escapes them into '('. Without @, the expression would look [baring typoes] like:

("\\(\(\\(.*?\\)\)\\)");

Within the string is the actual regular expression:

\( [a open paren before] ( [begin capturing] \( [any open paren] [followed by] . [anything] * [0 or more . [anything]] ? [eh? This is unnecissary, or a capture convention I'm unfamiliar with] [followed by] \) [a close paren] ) [end capture block] \) [followed by a paren after]

[edit: For more on Regular Expressions, I suggest O'Reilly's "Mastering Regular Expressions" by Friedl. Includes in depth descriptions about how they work, how to use them, as well as dialect chapters for python, .NET, perl, and more.]
ok I got another question. lets say I got a string Test="abcdefg";
and I want to replace the 2nd element in the string with a "x". how can I do that
I tried this
Test=Test.Remove(1,1);
Test=Test.Insert(1,"x");
but that didnt work It removed the "b" just fine but it wont insert the "x" in the Test[1].

This topic is closed to new replies.

Advertisement