Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
More...


Forums » .NET » .NET »

How to split the string which is enclosed with quotes


Posted Date: 03 Jun 2009      Posted By:: Akshaya      Member Level: Silver    Member Rank: 0     Points: 1   Responses: 7



if i need to split with comma but dont split with comma when the words are inside the quotes, what do i do?


string Value = john,"aaa,code,book",phone;

my output should be like

john
"aaa,code,book"
phone


Thanks in Advance




Responses

#384938    Author: Anand Swami      Member Level: Gold      Member Rank: 695     Date: 03/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

Hi,

string Value = john,"aaa,code,book",phone;

You add the Line:

String OPut[]=Value.Split(",")

You declare the oput string or array.

Then Split the Value using the Split operator.

You Get the Oput[0]=john
OPut[1]="aaa,code,book"
Oput[2]=phone

--
Thanks & Regards,
Anand



 
#384950    Author: Akshaya       Member Level: Silver      Member Rank: 0     Date: 03/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

Hi,


This will give out put as

the Oput[0]=john
OPut[1]=aaa
OPut[2]=code
OPut[3]=book
Oput[4]=phone


But I don't want to split the string , which is their within Quotes.



 
#384996    Author: Vikranth Reddy      Member Level: Bronze      Member Rank: 0     Date: 03/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

Hi Archana

string Value = "john,'aaa,code,book',phone";
string[] OPut = Value.Split('\'');

This will works you can try

Vikranth Reddy Mula






 
#384998    Author: Akshaya       Member Level: Silver      Member Rank: 0     Date: 03/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

Hi Vikranth,

This will work only for 3 strings like above because of escape character, But when I increase the length of the string say from
string Value = john,"aaa,code,book",phone; to
string Value = john,"aaa,code,book",phone,"dhasjdh,jdhasjd",dhjasdh,dhasjdh,djhasjdh,djasdjasdj,dsauidh;

it does not work.

Please check



 
#385026    Author: Satish Kumar J      Member Level: Gold      Member Rank: 30     Date: 03/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

Following code can help you.


string Value = @"john,""aaa,code,book"",phone,""dhasjdh,jdhasjd"",dhjasdh,dhasjdh,djhasjdh,djasdjasdj,dsauidh";
//string Value = @"john,""aaa,code,book"",phone";
List<string> strList = new List<string>();
string tempString = "";
bool isQuoteStarted = false;
for (int i = 0; i < Value.Length; i++)
{
if ((int)Value[i] != 44 && (int)Value[i] != 34 && isQuoteStarted == false)
{
tempString += Value[i].ToString();
}
else
{
if ((int)Value[i] == 34)
{
if (isQuoteStarted)
{
strList.Add(@""""+ tempString + @"""");
isQuoteStarted = false;
tempString = "";
}
else
{
isQuoteStarted = true;
if (tempString != "")
{
strList.Add(tempString);
tempString = "";
}
}
}
else if ((int)Value[i] == 44 && isQuoteStarted == false)
{
if (tempString != "")
{

strList.Add(tempString);
tempString = "";
}
}
else
{
if (isQuoteStarted)
{
tempString += Value[i].ToString();
}
}
}
if ((i + 1) == Value.Length)
{
strList.Add(tempString);
}
}
for (int i = 0; i < strList.Count; i++)
{
MessageBox.Show(strList[i]);
}


Please let me know if you dont understand any logic in above code.

HTH

Regards,
Satish
My Blog



 
#385407    Author: Akshaya       Member Level: Silver      Member Rank: 0     Date: 04/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

HI Satish,

Thanks for your help , But the problem in this code is:

As you have check for empty string or null string , If any of the column is balnk like eg: test,hdh,"kfjsdkf,fksdjf,kfjds",,fsdf,,fsdf,gsd,gsdg,,,,,,

even though it has 14 fields, Result will result only 7 and also mistach in column data.
If you dont check for empty string , after escape sequence it will add another empty string and result will be 15 or 16 columns , which is again wrong.



Solution for this problem :


private ArrayList GetLineData(string line)
{
string chrString = null;
bool openSpeach = false;
ArrayList retVal = new ArrayList();
ArrayList finalValues = new ArrayList();
int quotesP = 0;
int index = 0;
int j = 0;
finalValues = new ArrayList();
if ((line.Length == 0) || line == " ")
return finalValues;
bool containSpeech = false;
openSpeach = false;
retVal.Add("");
for (int i = 0; i <= line.Length - 1; i++)
{
chrString = line.Substring(i, 1);//.Chars(i).ToString;
switch (chrString)
{
case "\"":
openSpeach = !openSpeach;
if (openSpeach)
{
retVal[index] += "";
}
break;
case ",":

if (!openSpeach)
{
finalValues.Add(retVal[index]);
j++;
retVal[index] = "";
//containSpeech=true;
}
else
{
retVal[index] += chrString;
}
break;
default:
if (containSpeech)
{
if (!openSpeach)
finalValues.Add(retVal[index]);
}
else
{
retVal[index] += chrString;
}
break;
}


}
finalValues.Add(retVal[index]);
return finalValues;

}



 
#385422    Author: Akshaya       Member Level: Silver      Member Rank: 0     Date: 04/Jun/2009   Rating: 2 out of 52 out of 5     Points: 2

Hi all,

Their is another solution.

Add namespace System.text.regularexpression
with following colde in method.


it works.

private void button1_Click(object sender, EventArgs e)
{
string Value = "john,\"aaa,code,book\",phone,other,data";
Regex re = new Regex("\\,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string[] values = re.Split(Value);
foreach (string val in values)
Console.WriteLine(val);
}



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Problem dealing with stored procedure
Previous : How we use pop up window in asp.net
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.