boyapati

Hai friends,

I have a string which consists of catalogue information meant for Table names and column names.

so i have to separate table names and column names from that string and store into diff. arrays.

is HashTable useful for me store tablenames and column names at a time.

i am giving you an example string.

string str = @"clinic@cid,pid,did#doctor@docname,docid,did,pid#drug@did,pid,eno,dname#
emp@eno,did,pid,ename#employee@eno,ename#hospital@hid,hname,pid,did,cid#
nstock@item,num,weight,price,sdate#patient@pid,eno,did,pname#
pet@age#pet1@age#pet2@age#stock@item,num,weight,price,sdate#
stock1@item1,num1,weight1,price1,sdate1#t1@sno,sname#
t11@pkey#t12@pkeys#t2@sno,sval,ssname#temp@age#temp01@key1#temp07@key1#
temp1@key1#temp10@key2#temp2@key2#temp3@pkey#temp4@pkey#temp5@pkey#
temp6@pkey#temp7@key1#temp8@key2#temp9@key1#ustock@item,num,weight,price,sdate#"
;

here, "CLINIC" is a table name. And values between @ and # are column names.

thanks and regards,

prem


<Moderator: edited for easier reading>



Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

TilakGopi

Hi,

Here is the code , u require:

[Code language = "C#" ]

string str = @"clinic@cid,pid,did#doctor@docname,docid,did,pid#

drug@did,pid,eno,dname#emp@eno,did,pid,ename#employee@eno,ename#

hospital@hid,hname,pid,did,cid#nstock@item,num,weight,price,sdate#

patient@pid,eno,did,pname#pet@age#pet1@age#pet2@age#

stock@item,num,weight,price,sdate#

stock1@item1,num1,weight1,price1,sdate1#t1@sno,sname#

t11@pkey#t12@pkeys#t2@sno,sval,ssname#temp@age#

temp01@key1#temp07@key1#temp1@key1#temp10@key2#temp2@key2#

temp3@pkey#temp4@pkey#temp5@pkey#temp6@pkey#temp7@key1#

temp8@key2#temp9@key1#ustock@item,num,weight,price,sdate#";

string[] strTablesAndCols = str.Split(new char[] { '#' });

int nCount = strTablesAndCols.Length;

string[] strTables = new string[nCount];

string[][] strColumns = new string[nCount][];

Console.WriteLine("\tTables\t\tColumns");

Console.WriteLine("\t------\t\t-------");

for (int index = 0; index < nCount; index++)

{

    if (strTablesAndCols[index].Trim().Length > 0)

    {

        string[] strTokens = strTablesAndCols[index].Split(new char[] { '@' });

        strTables[index] = strTokens[0];

        Console.Write("\t"+strTables[index] + "\t\t");

        strColumns[index] = strTokens[1].Split(new char[] { ',' });

        for (int i = 0; i < strColumns[index].Length; i++)

            Console.Write(strColumns[index]Idea+" , ");

        Console.WriteLine();

    }

}

[/Code]

Hope, u r done.If any issues, feel free to get back.

Thanx,

Ch.T.Gopi Kumar.

(Moderator: Edited data line by adding @ before string which should not interfere with the example when processing. If it does concatenate it one line>






Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

Jay Vora

ohh its classic..

great work done sir...simply superb..

thanks..






Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

boyapati

when i am trying to print this instead of column names i am getting System.String[].

wht do i do





Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

boyapati

when i am trying to print this instead of column names i am getting System.String[].

wht do i do





Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

boyapati

Hai my dear friend,

Slightly, I changed ur code... and got solution perfectly.

Thank you my dear.

Prem boyapati.





Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

TilakGopi

Hi,

which line of code is making this prolbem

for (int i = 0; i < strColumns[index].Length; i++)

i guess it may be 'Console.Write(strColumns[index]Idea+" , "); '.

 

Actually it should be  Console.Write(strColumns[index][ i ]+" , ");

 

When the code is posted on this forum[ i] is getting converted to this symbol Idea .That's the problem.

 

If this is the problem , i gave u the solution.

 

If not, post me the line of code giving ur problem

 

Also , don't mind , i wanna know what change u have made and got the code working.

 

and welcome  always.

 

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

OmegaMan

The string data is in a definable pattern. If you are interested in acquiring the data out of a Regular Expression, it can be used to to parse it out, by drilling down into the matches, then captures one can extract the information.

Each match is one table and its columns. The regex pattern places the need data items into named groups for easy access. Its just matter of drilling down and acquiring the info.
Input Textclinic@cid,pid,did#doctor@docname,docid,did,pid#drug@did,pid,eno,dname#

Regular Expression( <Table>[^@]*)           # Get table name into group 'Table'
( :@)                     # Match but don't capture @
 ( <Columns>               # Group for all Columns
       (
        ( <Cols>[^,\#]*)  # Capture the individual arguement.
        ( :,)             # (MBDC) a comma
       )+                 # 1 to many of these arguements.
    ( <Final>\#)          # (MBDC) The final Anchor.
    ( <-Cols>)            # Pop off the final null object.
)                     

Group CapturesGroups: (0) (1) (Table) (Columns) (Cols) (Final)



Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

boyapati

Hai My dear friend,

Thank You... I have already got soln n executing properly. But urs also nice.

If urs is better to above one when we talk about performance.

regards,

Prem.





Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

OmegaMan

boyapati wrote:

Hai My dear friend,

Thank You... I have already got soln n executing properly. But urs also nice.

If urs is better to above one when we talk about performance.

regards,

Prem.



Note if one uses regex and the operation is used multiple times, set the regex option of Compiled for better performance. Good luck!





Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

boyapati

Hai,

yah, I am using Regex only.

thank you.





Re: Visual C# General Hai... I have a string. i have to read char by char...so which method is better one.

TilakGopi

Thanks Jay ,

Best Regards,

Ch.T.Gopi Kumar.