Appending Quations to a string
-
Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.
Replace
perhaps? But why create the string with the@
s rather than the characters you want? Creating the string the way you want shouldn't be a problem. Perhaps you should explain what you want and show the code you have so far. -
Replace
perhaps? But why create the string with the@
s rather than the characters you want? Creating the string the way you want shouldn't be a problem. Perhaps you should explain what you want and show the code you have so far.try
{//OleDbCommand oleDbCmd = new OleDbCommand("Select \* From \[Sheet1\]", oleDbCon); //oleDbDa.SelectCommand = oleDbCmd; oleDbDa.Fill(ds,"ServerName"); DataTable dt = ds.Tables\["ServerName"\]; //string str1stValue = ds.Tables\[0\].Rows\[0\]\[0\].ToString(); foreach(DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) strServerName.Append(dr\[dc\].ToString()).Append("\\",\\""); //strServerName.AppendFormat("\\",\\"", dr\[dc\].ToString()); } //strFinalList = strFinalList.AppendFormat("\\",\\"", strServerName); Response.Write(strServerName); }
But here again i need to get rid of of the quotations at the end of the string and need to add it before the string. The result that is get is, abc","def","ghi","jkl"," As I had mentioned i'd like to get the result as, "abc","def","ghi","jkl"
-
try
{//OleDbCommand oleDbCmd = new OleDbCommand("Select \* From \[Sheet1\]", oleDbCon); //oleDbDa.SelectCommand = oleDbCmd; oleDbDa.Fill(ds,"ServerName"); DataTable dt = ds.Tables\["ServerName"\]; //string str1stValue = ds.Tables\[0\].Rows\[0\]\[0\].ToString(); foreach(DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) strServerName.Append(dr\[dc\].ToString()).Append("\\",\\""); //strServerName.AppendFormat("\\",\\"", dr\[dc\].ToString()); } //strFinalList = strFinalList.AppendFormat("\\",\\"", strServerName); Response.Write(strServerName); }
But here again i need to get rid of of the quotations at the end of the string and need to add it before the string. The result that is get is, abc","def","ghi","jkl"," As I had mentioned i'd like to get the result as, "abc","def","ghi","jkl"
How about:
foreach(DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
strServerName.AppendFormat( "\"{0}\"," , dr[dc] );
}
strServerName.Length-- ; -
Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.
-
try
{//OleDbCommand oleDbCmd = new OleDbCommand("Select \* From \[Sheet1\]", oleDbCon); //oleDbDa.SelectCommand = oleDbCmd; oleDbDa.Fill(ds,"ServerName"); DataTable dt = ds.Tables\["ServerName"\]; //string str1stValue = ds.Tables\[0\].Rows\[0\]\[0\].ToString(); foreach(DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) strServerName.Append(dr\[dc\].ToString()).Append("\\",\\""); //strServerName.AppendFormat("\\",\\"", dr\[dc\].ToString()); } //strFinalList = strFinalList.AppendFormat("\\",\\"", strServerName); Response.Write(strServerName); }
But here again i need to get rid of of the quotations at the end of the string and need to add it before the string. The result that is get is, abc","def","ghi","jkl"," As I had mentioned i'd like to get the result as, "abc","def","ghi","jkl"
What Piebald said; or else what I often do when I do not want the StringBuilder:
string result="";
string sep="";
foreach(string s in strings) {
result+=sep+"\""+s+"\"";
sep=",";
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
What Piebald said; or else what I often do when I do not want the StringBuilder:
string result="";
string sep="";
foreach(string s in strings) {
result+=sep+"\""+s+"\"";
sep=",";
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
I'd still use
String.Format
-- I find it easier to read than a bunch of concatenations. -
How about:
foreach(DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
strServerName.AppendFormat( "\"{0}\"," , dr[dc] );
}
strServerName.Length-- ;Thanks PIEBALDconsult for the help. The solution is just perfect, but there is an extra comma at the end of the string, "abc","def","ghi","jkl", How could I get rid of the last comma?
-
I'd still use
String.Format
-- I find it easier to read than a bunch of concatenations.I'd like to go with PIEBALDconsult on this one, but thanks for the help :)
-
I'd still use
String.Format
-- I find it easier to read than a bunch of concatenations.I use it sparingly,
string.Format
I mean. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Thanks PIEBALDconsult for the help. The solution is just perfect, but there is an extra comma at the end of the string, "abc","def","ghi","jkl", How could I get rid of the last comma?
You probably left off the decrement of the Length.
-
You probably left off the decrement of the Length.
oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string. It is just like the SQL statement that we write. e.g. Select * from table where name like ('abc','def','ghi') I am trying to send the variable in this format to the stored procedure.
-
oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string. It is just like the SQL statement that we write. e.g. Select * from table where name like ('abc','def','ghi') I am trying to send the variable in this format to the stored procedure.
It should be outside the loop, as in the example I posted.
-
oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string. It is just like the SQL statement that we write. e.g. Select * from table where name like ('abc','def','ghi') I am trying to send the variable in this format to the stored procedure.
Arun Philip Reynolds wrote:
It is just like the SQL statement that we write.
e.g. Select * from table where name like ('abc','def','ghi')If you are using SQL Server (and you should), then how about... SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]
-
I use it sparingly,
string.Format
I mean. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
I use it whenever I can -- muaa haa ha ha! :cool:
-
Arun Philip Reynolds wrote:
It is just like the SQL statement that we write.
e.g. Select * from table where name like ('abc','def','ghi')If you are using SQL Server (and you should), then how about... SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]
Thanks, but I just tried this,
strHolderString = strServerName.ToString();
strHolderString = strHolderString.TrimEnd(',');
strFinalList = new StringBuilder(strHolderString);here the strHolderString is a string variable and I am getting the result that I was looking for. Thanks once again to everyone. :-D
-
What Piebald said; or else what I often do when I do not want the StringBuilder:
string result="";
string sep="";
foreach(string s in strings) {
result+=sep+"\""+s+"\"";
sep=",";
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
strHolderString = strServerName.ToString();
strHolderString = strHolderString.TrimEnd(',');
strFinalList = new StringBuilder(strHolderString);I just tried the above and it works for me. Here the strHolderString is a string variable and I used the TrimEnd function to get rid of the trailing comma. The strFinalList is a StringBuilder variable. Thanks once again to all
-
Thanks, but I just tried this,
strHolderString = strServerName.ToString();
strHolderString = strHolderString.TrimEnd(',');
strFinalList = new StringBuilder(strHolderString);here the strHolderString is a string variable and I am getting the result that I was looking for. Thanks once again to everyone. :-D
:sigh: Just decrement the Length. Why create two Strings and another StringBuilder?
-
Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.
You can use SmartPaster addin for visual studio.Copy the text you needed and paste it as string builder.Here is the code and installer http://www.martinwilley.com/blog/2010/06/06/SmartPasteIn2010.aspx[^]
-
What Piebald said; or else what I often do when I do not want the StringBuilder:
string result="";
string sep="";
foreach(string s in strings) {
result+=sep+"\""+s+"\"";
sep=",";
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.
An alternative approach is to use a list of strings instead of a
StringBuilder
to build your list and then concatenate them as you need. Here's one way to do this:public class FormattedStrings : List<string>()
{
public override string ToString()
{
StringBuilder sb = new StringBuilder();
bool started = false;
foreach (string item in this)
{
if (started)
{
sb.AppendFormat(",\"{0}\"", item);
}
else
{
started = true;
sb.AppendFormat("\"{0}\"", item);
}
}return sb.ToString();
}
}I am assuming that you have assigned the
@
symbol when you built yourStringBuilder
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility