Last month, we went back to basics with the Int class. This month, we look at the textual equivalent, the string class.
The String & Int data types, single-handed, are the two most used data in the .NET platform. Between them, they handle about 90% of all the data we use.
Strings are nothing complex; they are just simple sequences of characters that make up words and sentences. The string class exists to allow us to chop up these sentences, replace parts of them, search them, and a whole bunch of other things that make handling strings of text easier for us as developers.
So, what can a string do?
The string class has a phenomenal amount of functionality in it, all of which is grouped into about three categories as follows:
We'll start with "String Tests."
When we talk about string tests, we are in fact talking about checking to see if a string is present, or if that string has a certain sub string contained within it.
Many developers will instantly recognize that, in this case, an appropriate test would be:
if (myname == "shawty") { }
String, like most classes that are also basic data types, implements the equality operator, allowing you to perform simple straight-forward tests like this inline in your application code.
There is, however, a number of useful specialist tests too. Consider this fragment of code:
if (myname == "" && myname == null) { }
In language terms, we are saying, IF the variable myname has no contents, that is it is empty and if it is null, that is devoid of any value, then consider our decision logic to be true.
The string class makes this much easier with a static method called 'IsNullOrEmpty'
if (String.IsNullOrEmpty(myname)) { }
To me, the string method version reads much better, and makes better sense from a syntax point of view because the name of the expression tells you the exact test you're performing.
Other tests are usually performed as an extension to the string itself. For example 'contains':
string myname = "shawty";
if (myname.Contains("shaw")) { }
Will pass, because 'shawty' does contain 'shaw'.
string myname = "shawty";
if (myname.Contains("peter")) { }
will, however, fail. Rather than looking in the string, if you're looking for prefixes or suffixes, 'StartsWith' and 'EndsWith' have your back covered.
string myname = "Mr Peter Shaw";
if (myname.StartsWith("Mr") &&
myname.EndsWith("Shaw")) { }
will become true if the first two characters are equal to "Mr" AND the last four are equal to "Shaw" but won't care about any of the other contents in the string. It's also a point to note that the test IS case sensitive, so "Mr" will never match "mr." I soon will show you a way to deal with this, however.
All of these tests can also be negated, so to test if a string does NOT start with "Mr" it's as simple as prefixing the method call with an exclamation mark.
string myname = "Mr Peter Shaw";
if (!myname.StartsWith("Mr")) { }
The String & Int data types, single-handed, are the two most used data in the .NET platform. Between them, they handle about 90% of all the data we use.
Strings are nothing complex; they are just simple sequences of characters that make up words and sentences. The string class exists to allow us to chop up these sentences, replace parts of them, search them, and a whole bunch of other things that make handling strings of text easier for us as developers.
So, what can a string do?
The string class has a phenomenal amount of functionality in it, all of which is grouped into about three categories as follows:
- String tests
- Searching
- String modification and building
We'll start with "String Tests."
When we talk about string tests, we are in fact talking about checking to see if a string is present, or if that string has a certain sub string contained within it.
Many developers will instantly recognize that, in this case, an appropriate test would be:
if (myname == "shawty") { }
String, like most classes that are also basic data types, implements the equality operator, allowing you to perform simple straight-forward tests like this inline in your application code.
There is, however, a number of useful specialist tests too. Consider this fragment of code:
if (myname == "" && myname == null) { }
In language terms, we are saying, IF the variable myname has no contents, that is it is empty and if it is null, that is devoid of any value, then consider our decision logic to be true.
The string class makes this much easier with a static method called 'IsNullOrEmpty'
if (String.IsNullOrEmpty(myname)) { }
To me, the string method version reads much better, and makes better sense from a syntax point of view because the name of the expression tells you the exact test you're performing.
Other tests are usually performed as an extension to the string itself. For example 'contains':
string myname = "shawty";
if (myname.Contains("shaw")) { }
Will pass, because 'shawty' does contain 'shaw'.
string myname = "shawty";
if (myname.Contains("peter")) { }
will, however, fail. Rather than looking in the string, if you're looking for prefixes or suffixes, 'StartsWith' and 'EndsWith' have your back covered.
string myname = "Mr Peter Shaw";
if (myname.StartsWith("Mr") &&
myname.EndsWith("Shaw")) { }
will become true if the first two characters are equal to "Mr" AND the last four are equal to "Shaw" but won't care about any of the other contents in the string. It's also a point to note that the test IS case sensitive, so "Mr" will never match "mr." I soon will show you a way to deal with this, however.
All of these tests can also be negated, so to test if a string does NOT start with "Mr" it's as simple as prefixing the method call with an exclamation mark.
string myname = "Mr Peter Shaw";
if (!myname.StartsWith("Mr")) { }
very informative blog and useful article thank you for sharing with us , keep posting learn
ReplyDeletemore about Dot net
.NET Online Training Bangalore