Go to DotNetFunda.com
 Online : 1571 |  Welcome, Guest!   Login
 
Home > Articles > C# > Best Practise in String Methods

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

Best Practise in String Methods

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

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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.




If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

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
Level:Starter
Status: [Member]
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 | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
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)