SIGN UP MEMBER LOGIN:    
ARTICLE

Effective C#: Working with Strings

Posted by Mahesh Chand Articles | Coding Best Practices March 12, 2001
Using string might degrade the performance of your application. This article explains about what precautions you should take when you are going to use strings in your application.
Reader Level:

Empty String

Checking if a string is empty or not is a basic programming need. The effective way to check if a string is empty or not is to use the Length property of the string class, instead of using null or comparing with " " string.

string str1 = AMethodReturnsString()
{

// Do something and return a string
}
if (str1.Length >0 )
{
// do something
}

String Concatenation

Whenever you modify a String it returns a new string as the result. Creating many String objects might degrade the performance of your program. You can avoid creating a new instance of String by using the StringBuilder class.

Say you want to concatenate two strings. Here is the traditional way -

string str1 = "I like ";
string str2 = "Soccer";
string strConcat = string.Concat(str1, str2);

The value of strConcat = "I like Soccer". You can use the StringBuilder class and its Append method to do the same.

StringBuilder MyStrBuilder = new StringBuilder ("I like ");
String newStr = "Soccer";
MyStrBuilder.Append(newStr);

The value of MyStrBuilder is "I like Soccer".

Comparing String

Use string.Equals method to compare two strings.

string str1 = AMethodReturnsString()
if (str1.Equals("TestSting") )
{
// do something
}

Login to add your contents and source code to this article
share this article :
post comment
 

its simple. A string is a sequence of characters. you can access them through their index. string s="hai chand"; char c=s[0]; Console.Write(c); that's it

Posted by Ranganath prasad Dec 29, 2010

string s = "hi chand";


i want to select first character into 
char c;

how can i do this..

reply me asp with explanation... pls...

Posted by vinay kumar Jun 21, 2010

I feel using string.IsNullOrEmpty() is better way of checking if the string is empty or not.

Posted by Ujwol Shrestha May 25, 2010

respected sir,

i want to compare two strings and display the larger string by ascii values!!!str1="discovery"and str2="geography" ..write a program 2 display the larger one
regards
aarif

Posted by mohamed aarif Sep 05, 2009

I need to evaluate an string value in order to know if is a valid number, integer if is possible. Unitl now I've been using a try catch block, but it is too long code. Thanks.

Posted by Javier Alvarez Nov 08, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor