Appending Quations to a string
-
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
-
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
Ew, yuck, duplicated code. X| Needless boolean, needless
if/else
. Stick the comma on the end and decrement it off when you're done. -
Ew, yuck, duplicated code. X| Needless boolean, needless
if/else
. Stick the comma on the end and decrement it off when you're done.To be fair, this was just knocked up in the CP editor and doesn't reflect what I would push out the door. I did look at doing it your way, but saw that you'd already posted that so it seemed that there wasn't much point to showing something that was already done - this is purely to stimulate the mind into thinking of other ways of solving the problem.
*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