1-May-07 (Created: 1-May-07) | More in 'CS-dotnet'

Working with c# strings?

Using the string builder for appending strings

public static string getHtmlForOrderInfoList(IList orderInfoList)
{
  string header = "some html header";
  string rows = "text{0}somemore text{1}otherstuff{2}";
  string trailer = "trailer";
  StringBuilder finalString = new StringBuilder(header);
  foreach(PANOrderInfo oi in orderInfoList)
  {
    finalString.Append(string.Format(rows
       ,oi.m_orderRefNumber
       ,oi.m_requirementDateTime
       ,oi.m_orderId));
     }
    finalString.Append(trailer);
    return finalString.ToString();
}

Splitting strings

char[] sepString = new char[]{'1'};
string[] userIdList = customerIdList.Split(sepString);

>> Monday, July 19, 2004 4:00:46 PM - Comments by satya

Take a look at this microsoft reference

When should I use == and when should I use Equals?

Based on that discussion


String s1 = "abc";
String s2 = "abc";
if (s1 == s2)
{
	// this will be true
}

Object o1 = s1;
Object o2 = s2;
if (o1 == o2)
{
	//this will be false
} 

// When in doubt use Equals
if (s1.Equals(s2) { //true }}
if (o1.Equals(o2) { //true as well }}

Working with relational operators

C# Intro: Operators, expressions, and conditionals from DevHood


//you can do this
s1 = "abc";
if ( (s1 == "") || (s1 == "abc") ){}