Go to DotNetFunda.com
 Online : 156 |  Welcome, Guest!   Login
 Skip Navigation Links Home > Articles > C# > Best Practise in String Methods
  Win at least 8 gifts every month now! >> Top Performers of this month.   

  • Video Tutorials is live !. Learn ASP.NET, UML, LINQ, Sharepoint etc. through videos now.
  • This month, we are distributing prizes worth Rs. 18,750/- (estimated) including Rs. 3000/- cash. Write for us and win!.
  • Advantages and disadvantages of working in small and big company, advice from our expert panels. Read here.


Notice: If page content has been copied from other sources without proper permission, kindly let us know with details from where it has been copied for further action.

All Articles | Submit Article |

Best Practise in String Methods

 Posted on: 7/6/2009 5:45:52 AM by Cp_raj | Views: 656 | Category: C# | Level: Beginner | Print Article
String Method

Webmaster says - If this article content has been copied from other websites without proper permission, kindly let us know with details from where it has been copied for further action.

Introduction
I just take this as a sample for best practices.Still lots of methods avail in C#.



String.isNullOrEmpty()

During coding to check the string whether it is null or empty in .net 1.1 we use to check like this.

if (s != null && s != "")

{

return "Is not null or empty";

}

else

{

return "Is  null or empty";

}

But in .Net 2.0 framework they introduced  String.IsNullorEmpty (). It will return true if the string variable is empty or Null. Use this as best practice.

While doing code review using FxCop if you use the old method(if (s != null && s != "")) it will advice your to use String.IsNullorEmpty() method.

if (string.IsNullOrEmpty(s) == true)

{

return "Is null or empty function";

}

else

{

return "Is not null or empty function";

}



String.Compare()

To Compare string

We use to code like this.

string s = "abcd";

string s1 = "Abcd";

if (s==s1)

{

return "both are equal";

}

else

{

return "Both are not equal";

}

O/P – Both are not equal.

Even in .net 1.1 we have a method call Compare().It has lot of overloaded methods in that I have taken (string,string,bool ignorecase).To compare string use this.The last parameter is to check the case sensitive.If you give “true” then it will ignorecase sensitive.

 

if (string.Compare(s, s1, false) == 0)

{

return "both are equal";

}

else

{

return "Both are not equal";

}

O/p : Both are not equal.




Conclusion

In .Net 3.5 Microsoft had added lots of new methods. If any of the string method is not available use Extension methods available in .Net 3.5.




Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:8 year(s)
Home page:http://www.dotnetfunda.com
Member since:Tuesday, May 26, 2009
Biography:I started my career with Asp,ms-access,sql server 7,xml.Then i updated to .net,share point server,Basics about Biztalk server
 Latest post(s) from Cp_raj

   ◘ Best Practise in String Methods posted on 7/6/2009 5:45:52 AM
   ◘ Extension Method (.Net 3.5 and above) posted on 6/29/2009 6:27:15 AM


Submit Article

About Us | The Team | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)