About High Level string parsing in C#

About High Level string parsing in C#

Post by kiplrin » Sun, 11 Sep 2005 00:52:58



1. I have a data like this.

16;,1,2;;3,4,;5,6;,,

The result what I want is
16 1 2 3 4 5 6( remove all ;,: and each word is seperated exactly one
empty space)

How can I do it?

2. I have a LONG data like this

1,2,3,4, ... , 10000

The function what I want is >> which works like this. It should be much
faster ReadLine() as a string and Split() it.
int a;
longData >> a; // a is 1
longData >> a; // a is 2
longData >> a; // a is 3

I searched it. But I couldn't find any result.
I beg your advice.

 
 
 

About High Level string parsing in C#

Post by kiplrin » Sun, 11 Sep 2005 00:56:21


p.s.
The Long data is saved file containing string data

 
 
 

About High Level string parsing in C#

Post by Narendiran » Sun, 11 Sep 2005 01:01:07


For the first case u can use the REPLACE() function of the String class.....
but the second question i cannot understand.
 Naren

Quote:

> p.s.
> The Long data is saved file containing string data

 
 
 

About High Level string parsing in C#

Post by kiplrin » Sun, 11 Sep 2005 06:12:40


1. solution
        public void FastTrim()
        {

            string data = "1,2,3,4;,5,;,5";

            data = data.Replace(",", " ");
            data = data.Replace(";", " ");

            string empty = " ";
            while( data.IndexOf( empty) >= 0)
            {
                empty += " ";
            }
            empty = empty.Substring(0, empty.Length - 1);

            while (empty.Length > 0)
            {
                data = data.Replace(empty, " ");
                empty = empty.Substring(0, empty.Length - 1);
            }

            MessageBox.Show(data);
        }

2. usage & solution( it is compiled with vs 2005, int? is a int that
can have the value "null", it is called nullable int)
       StreamReader sr = new StreamReader("test.txt");
       int? temp;
       while ((temp = ReadInt(sr)) != null)
       {
           MessageBox.Show(temp.ToString());
       }
       sr.Close();

       public int? ReadInt( StreamReader sr)
        {
            int intCh;
            char ch;

            string temp = "";
            while ((intCh = sr.Read()) != -1)
            {
                ch = (char)intCh;
                if (ch == ',' || ch == ';')
                {
                    if (temp.Length > 0)
                    {
                        break;
                    }
                }
                else
                {
                    temp += ch.ToString();
                }
            }

            if (temp.Length > 0)
                return int.Parse(temp);
            else
                return null;
        }

 
 
 

About High Level string parsing in C#

Post by www.pointnetsolutions.c » Mon, 12 Sep 2005 03:43:16



>1. solution
>        public void FastTrim()
>        {

>            string data = "1,2,3,4;,5,;,5";

>            data = data.Replace(",", " ");
>            data = data.Replace(";", " ");

>            string empty = " ";
>            while( data.IndexOf( empty) >= 0)
>            {
>                empty += " ";
>            }
>            empty = empty.Substring(0, empty.Length - 1);

>            while (empty.Length > 0)
>            {
>                data = data.Replace(empty, " ");
>                empty = empty.Substring(0, empty.Length - 1);
>            }

>            MessageBox.Show(data);
>        }

>2. usage & solution( it is compiled with vs 2005, int? is a int that
>can have the value "null", it is called nullable int)
>       StreamReader sr = new StreamReader("test.txt");
>       int? temp;
>       while ((temp = ReadInt(sr)) != null)
>       {
>           MessageBox.Show(temp.ToString());
>       }
>       sr.Close();

>       public int? ReadInt( StreamReader sr)
>        {
>            int intCh;
>            char ch;

>            string temp = "";
>            while ((intCh = sr.Read()) != -1)
>            {
>                ch = (char)intCh;
>                if (ch == ',' || ch == ';')
>                {
>                    if (temp.Length > 0)
>                    {
>                        break;
>                    }
>                }
>                else
>                {
>                    temp += ch.ToString();
>                }
>            }

>            if (temp.Length > 0)
>                return int.Parse(temp);
>            else
>                return null;
>        }

Why not use a FileStream ? and regex ?
 
 
 

About High Level string parsing in C#

Post by kiplrin » Sun, 18 Sep 2005 11:24:48


hm... because I don't know how to use it.
In fact, I hate regular expression except "Replace" dialogue in VS .net.
 
 
 

1. C# - parsing an XML string

Hello,

I am developing in Visual Studio .NET 2005.  I am attempting to parse
an XML string by using the example given in the help files:

ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref/html/M_System_Xml_XmlReader_Create_1_6b76289f.htm

namely,

string xmlData ="<item productID='124390'>" +
                        "<price>5.95</price>" +
                        "</item>";

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(new StringReader(xmlData));

However, when I attempt to actually parse this string by

reader.ReadStartElement("item");
string productID = reader.GetAttribute("productID");

I just get null for productID.  Can anyone see what I am doing wrong?

Thanks,
Don

2. Help - Multi booting and FAT32

3. How to Parse Xml String in DotNet 2003 C#.

4. change permission

5. high-level profiler for asp.net applications

6. ANNOUNCE: First 3 turns free for Storm of the Eye.

7. Are High-Level GetNamedSecurityInfo APIs broken?

8. Cart port advice

9. Delegate Function - Scenarios or high level explanation

10. Any high-level profilers available for .net

11. .NET Add-ins in Excel do not show up in Add-ins

12. XML parse error: Invalid at the top level of the document

13. Add-Ins for office using vs7 and c#