Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   C# String Manipulation Tutorial With Examples (http://www.go4expert.com/articles/c-sharp-string-manipulation-tutorial-t30015/)

shabbir 23Feb2014 11:02

C# String Manipulation Tutorial With Examples
 
Strings are an integral part of any programming language. In almost every programming language you would find strings of data being passed as input and being displayed as input. Strings are an important way to interact with the user.

Whenever you visit a website, you are often asked to sign up into the website or login if you are already a member of the website. You enter a username and password, system performs some validation checks and you are good to go. The process might look pretty straight forward to you at that moment but behind the screen lots and lots of string handling is being done. Similarly, database applications make huge use of string and actually strings are the intermediate between the user and the database for information storage.

This article is dedicated to string handling and text manipulation in C#. We will show you how actually string resides in the memory. What are the build in methods that you can call on the string class and where can they be used. After completing this article, you will have a better idea about how the login systems work and what happens to the strings that you enter there. Things you will learn in this article will help you understand and implement strings in your code in a better way. So, without wasting any further time on theory, let us get straight to the task.

String Construction



Constructing a string in C# is an extremely easy task. Strings are actually class belonging to the System namespace. So actually what you are doing is creating instances of System.String class and applying different functionalities on that class.

The easiest and simplest way to declare a string in C# is to create an instance of a string class and then equate it to some literal value. A string is basically a sequence of characters. Following is the simplest way to construct a string in C#.

Code:

string name = “Expert”;
Code:

string description =”1-C#\n2-Java\n3-C++”;
Code:

string content =@”8fsf8s\\sdf9sf_’’asaa”;
In order to create a sequence of characters, for instance some special characters you can simply use string’s constructor as

Code:

string stars = new string( ‘*’,15);
The above line of code will generate a string with 15 asteric characters.

We mentioned earlier that strings are basically sequence of characters. Therefore, we can convert a string into a character array using ToCharArray method and we can convert array of characters back to a string by passing it into the constructor of the string as follows.
Code:

char  [] arr = “Expert”.ToCharArray;
string name = new string (arr);

Let us recapitulate the concepts we have studied till now with the help of some example. Have a look at Example1.

Example1

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial {
    class Program {
        static void Main() {
            string name = "Expert";
            Console.WriteLine(name);
            string description ="1-C#\n2-Java\n3-C++";
            Console.WriteLine(description);
            string content = @"8fsf8s\nsdf9sf_’’asaa";
            Console.WriteLine(content);
            char [] arr= "Expert".ToCharArray();
            string array = new string(arr);
            Console.WriteLine(array);
            Console.ReadLine();
        }
    }
}

The code in Example1 is simply practical implementation of string construction concept. We have created a string name and displayed it, then we created a string description to give you an idea of how we can use \n to go to a new line within a string. And next in the content string we have displayed that if you want to includes special characters in the string as it is, for example if you want to insert \n, it will shift the control to next line but if you want to display \n in a string you can simply prefix a @ character before the string literal and it will display the string as it is. In next lines we declared an array of characters and passed it to the constructor of the string. In the next line we have displayed the value of that string. The output of code in Example1 would be as follows.

Ouput1

http://imgs.g4estatic.com/c-sharp/strings/output1.png

You can see in the fourth line that we have displayed a \n character within a string which if we don’t append @ before the string, would have shifted the control to the next line.

You can see that how simple it is to create and manipulate a string in C#. Now let us get to some other important concepts.

Empty and Null Strings



String can be empty, which means that it doesn’t contain any character. The length of an empty string will always be zero. Usually empty strings are used for validating if user has entered any data or not. We will show that later with the help of an example, but for now there are two ways to create an empty string.
  1. string name = “”;
  2. string name= string.Empty;
In both of the above cases an empty string will be stored in string variable name.
A null string is different from an empty string. String is a reference type which means that a string can be null. In order to point a string to null reference you simply have to do string name = null;

There is an important and useful method associated with strings that checks if a string is null or empty. The method is IsNullOrEmpty.

Now let us come towards our next example which will explain the concepts of null and empty strings.

Example2

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial {
    class Program {
        static void Main() {
            string name;
            Console.WriteLine("Enter your name:");
            name = Console.ReadLine();
            if (name == string.Empty)
                Console.WriteLine("The name field cannot be left empty.");
            else
                Console.WriteLine("There are " + name.Length + " characters in your name.");
            name = null;
            if(string.IsNullOrEmpty(name))
                Console.WriteLine("Name has been set to null.");
            Console.ReadLine();
        }
    }
}

Example 2 is another simple example. We simply ask the user to enter his name. If user doesn’t enter anything, it will display the message to the user that string you entered is empty which is not allowed. This is a typical login scenario where you enter your user name and password and if any of them is empty, the application prompts with an error. In those application database comparisons are being done but now for the sake of understanding we limit it to string comparison. Coming back to our problem, hand if user enters any string, we will display the length of that string. Next we will make our string null and will call IsNullOrEmpty static method on that string to verify if the string is really null or not. The output of the above code will be as follows.

Output2

http://imgs.g4estatic.com/c-sharp/strings/output2.png

String Searching



Searching characters or substrings within a string is another extremely important task while dealing with strings. For example, if you want to find out that if a string contains a specific words or specific character or if you want to find out if a string ends with a particular word or not you can make use of string searching functions. Although, string type contains numerous searching functions along with their overloads, we will study the ones that are most commonly used.

In order to access a character within a string you can simply use array style index searching. Strings are basically sequence of characters; therefore you can access character within a string by its index. For example
Code:

string name = “Expert”;
char c = name[1]    // This will return ‘x’, because x is located at first index of Expert

Simply, in order to search strings within a string or more commonly known as substrings. You can make use of three basic functions.

1. Contains

This function is used to search a substring in a string. For example
Code:

string name = “I am a CSharp Expert”;
Console.WriteLine (name.Contains(“CSharp”));

This will evaluate to true because string name contains substring “CSharp”

2. StartsWith

This function is used to search a substring in at the start of a string. For example
Code:

string name = “James is  a CSharp Expert”;
Console.WriteLine (name.StartsWith(“James”));

This will evaluate to true because string name starts with “James”

3. EndsWith

This function is used to search a substring in at the end of a string. For example
Code:

string name = “James is  a CSharp Expert”;
Console.WriteLine (name.EndsWith(“Expert”));

This will evaluate to true because string name ends with “Expert”

These are the three basic methods for finding a substring. Another extremely important method is that of IndexOf method which is used to find the starting index of a substring. For example, if you have a string named
Code:

string name = “expert”;
Console.WriteLine(name.IndexOf(“per”));

This will display the value 2, because the starting index of substring per is 2 in the string expert.

Now, as usual, we will explain all of the string searching concepts that we have studied till now with the help of an example. Have a look at Example3 to further understand the aforementioned string searching functions.

Example3

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace CSharpTutorial
{
    class Program
    {
      static void Main()
        {
            string name = "I am a CSharp Expert";

            Console.WriteLine("Calling Contains Function ...");
            Console.WriteLine(name.Contains("CSharp"));

            name = "James is  a CSharp Expert";
            Console.WriteLine("Calling StartsWith Function ...");
            Console.WriteLine(name.StartsWith("James"));

            Console.WriteLine("Calling EndsWith Function ...");
            Console.WriteLine(name.EndsWith("Expert"));

            name = "Expert";
            Console.WriteLine("Calling IndexOf Function ...");
            Console.WriteLine(name.IndexOf("per"));

            Console.ReadLine();
        }
    }
}

The code in Example3 is the implementation of what we studied in theory. We have simply created a string name and have then called corresponding Contains, StartsWith and EndsWith function on that string. Finally we called IndexOf function. You will see in the output that all of the functions were evaluated to true in the Console.WriteLine statement because Contains found Charp, StartsWith found James and EndsWith found Expert in the string name. In the end, the Index of method will fetch the starting index of the string ‘per’ and will display that on the output screen. The output of the above code is as follows.

Output3

http://imgs.g4estatic.com/c-sharp/strings/output3.png

An extremely important thing to note here is that if you change the case of the string while calling Contains, StartsWith and EndsWith method.

For instance in the Example3, if you make following changes while calling the StartsWith method
Code:

string name = "James is  a CSharp Expert";
Console.WriteLine("Calling StartsWith Function ...");
Console.WriteLine(name.StartsWith("JAMES"));

Now this method will evaluate to false. Though string ‘name’ starts with ‘James’ but in the StartsWith method we are comparing the string ‘JAMES’ all uppercase to the actual string. Therefore, this will evaluate to false. But there is a very simple solution to this problem. Contains, StartsWith and EndsWith methods come with overload using which you can ignore the case while comparing the string.

If you use following overload of these methods, you can compare the strings irrespective of the case.
Code:

string name = "James is  a CSharp Expert";
Console.WriteLine("Calling StartsWith Function ...");
            Console.WriteLine(name.StartsWith("JAMES",true,System.Globalization.CultureInfo.InvariantCulture));

Basically, the first argument is the string to compare; the second argument is Boolean variable ignorecase, if true, the case of the string being compared will be ignored, if false, the case of the string will be taken into account.

Pay particular attention to the third argument, CulturalInfo is a type in System.Globalization namespace. We have passed the InvariantCulture property of this type as a third argument in StartsWith method. It will actually make the method insensitive to the region or location in which the StartsWith method is being used. This is particularly important, because English language strings might work differently in different languages and regions. Therefore, if you want your application strings to behave uniformly in different regions, you can use this overload.

Another way to control the case of the comparison is to use another overload of these methods. Make the following changes in your code.
Code:

string name = "Expert";
Console.WriteLine("Calling IndexOf Function ...");
Console.WriteLine(name.IndexOf("PER"));

Now, the index returned would be some negative value, because IndexOf method is not able to find ‘PER’ substring inside the “Expert” string due to case sensitivity. There is another way to control case sensitivity while comparison. Make following changes in the code.
Code:

string name = "Expert";
Console.WriteLine("Calling IndexOf Function ...");
Console.WriteLine(name.IndexOf("PER",StringComparison.InvariantCultureIgnoreCase));

This code will again ignore the case of the string passed inside the IndexOf method.

String Manipulation



String manipulation is a process of calling a method on a string instance and retrieving another string. Strings are immutable in nature which means that whenever a string is manipulated a new string instance is created in the memory and returned back. The actual string instance stays as it is. StringBuilder class is a string variant that is mutable; we will see that later. For now, let us discuss some string manipulation functions.

1. Substring

Substring, method returns portion of string based on the indexes passed to it. For example
Code:

string name = "Expert";
string sub = name.Substring(1, 3);

This function will fetch the substring from index 1 of string ‘Expert’ to index ‘3’. The output of this will be ‘xpe’.

If you pass only one parameter to Substring function of the string, it will omit those number of characters from the string starting from the first index. Consider the following lines of code.
Code:

string name = "Expert";
string sub = name.Substring(2);

The string sub will contain ‘pert’, because the first two characters ‘Ex’ would be omitted by the substring function when we pass single integer parameter ‘2’ to it.

2. Insert

Another important function which is used in string manipulation is the Insert function. This function is used to insert string inside another string, starting from the specified index. Have a look at the example.
Code:

string name = "Expert";
string sub = name.Insert(2, "--");

Here we have a string ‘Expert’ stored in string instance name. We call insert function on this string and pass it parameters 2 and ‘–‘, respectively. It means that starting from the index 2, insert string ‘--’ into it. The output string ‘sub’ will be “Ex--pert”.

3.Remove

Remove is another important, yet simple to use string manipulation function. Remove basically allows you to remove a substring from main string. The first parameter is the starting index from where you want to remove the string and the second parameter is the number of characters you want to remove. Consider following lines of code.
Code:

string name = "Ex--pert";
string sub = name.Remove(2, 2);

Here we have an input string ‘name’ that contains string “Ex--pert”. We want to remove two dashes between the Ex and pert. We can simply pass 2 as a first parameter telling the compiler that we want to start removing string from index 2 and then 2, telling that 2 characters from that index.

4. PadLeft and PadRight

PadLeft and PadRight are used to pad certain character on the left and right of a string respectively. The first parameter is the number of character to pad and the second parameter is the character to pad. If you pass only one parameter i.e. number of characters, spaces would be padded. Consider following code.

This code will append 5 sterics on the right of string name.
Code:

string name = "Expert";
string sub = name.PadRight(5,'*');

And this code will append 5 spaces on the left of the string name.
Code:

string name = "Expert";
string sub = name.PadLeft(5);

5. Trim

Trim function is used to trim specified characters from the start and end of the string. If characters are not specified, it will trim white spaces from the start and end. TrimStart and TrimEnd method trim white spaces from only start and end of the string respectively. Have a look at following code.
Code:

string name = "  I am a CSharp expert  ";
string sub = name.Trim();

If we display the ‘name’ string, the white spaces at the beginning and end would not be there.

6. ToUpper and ToLower

These functions are used to convert a string into upper and lower case respectively. We will see the usage of this method in our detailed example.

Now, have a look at the Exampl1, we have implemented all the functions that we have studied till now in this Example1.

Example4

Code:

using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace CSharpTutorial
{

    class Program
    {
      static void Main()
        {

            Console.WriteLine("Using Substring function ...");
            string name = "Expert";
            string sub = name.Substring(1, 3);
            Console.WriteLine(sub);
            Console.WriteLine("===========\n");

            name = "Expert";
            sub = name.Substring(2);
            Console.WriteLine(sub);

            Console.WriteLine("===========\n");
            Console.WriteLine("Using Insert function ...");
            name = "Expert";
            sub = name.Insert(2, "--");
            Console.WriteLine(sub);

            Console.WriteLine("===========\n");
            Console.WriteLine("Using Remove function ...");
            name = "Ex--pert";
            sub = name.Remove(2, 2);
            Console.WriteLine(sub);

            Console.WriteLine("===========\n");
            Console.WriteLine("Using Trim function ...");
            name = "  I am a CSharp expert  ";
            sub = name.Trim();
            Console.WriteLine(sub);

            Console.WriteLine("===========\n");
            Console.WriteLine("Using ToUpper function ...");
            name = "Expert";
            sub = name.ToUpper();
            Console.WriteLine(sub);

            Console.ReadLine();

        }
    }
}

The code in Example1 is extremely simple. We have simply copy pasted the function examples into a working example to see how the program works and is the output of the program similar to what we studied in the examples. When you run this program you will see that all the outputs are according to our expectations and functions do their job as we discussed earlier. The output of this code is as follows.

Output4

http://imgs.g4estatic.com/c-sharp/strings/output4.png

Join and Split Strings



String joining and splitting are also handy. Often times you want to break a sentence into words or you want to join words to form a single string. String joining and splitting function basically allow you to do this. There are many important string manipulation function but we will study most important of them.

1. Split

Split method is used to split the strings. By default, it uses whitespaces as the parameter to split strings. Have a look at the following example.
Code:

string content = "I am a CSharp Expert";
string[] str = content.Split();

Now in the above case, ‘str’ array will contain words of the string ‘content’ at separate indexes.

This split method can be overloaded as well, for instance. You have a string string content = "I,am,a,CSharp,Expert";

And you want to split this string into words based on the comma, which means that you want that whenever a compiler reaches a comma it split the string and returns the number of words in the form of strings. This can be achieved by overloading the Split method and passing it the delimiting character which is comma in the above case. You can use Split function in following way in your code string[] str = content.Split(',');

2. Join

The Join function does exactly opposite of Split function. Join function takes a delimiting character and array of strings as an input and then join the words to form a string. Have a look at the following example.
Code:

string[] str = "I am a CSharp Expert".Split(' ');
string content = string.Join(",", str);

In the above code, basically we are splitting the string on the bases of a space. And then using Join function we are joining these words by inserting a comma between the words.

3. Concat and '+'

Concat is a static string method. It is similar to Join in functionality but It doesn’t take any delimiting character as a parameter. Its functionality is as follows.
Code:

string name = string.Concat("I ", "am ", " a", " CSharp", " Expert");
Plus operator “+” is also used to Join strings in a similar manner as illustrated by the following code snippet.
Code:

string  name = "I " + "am " + " a" + " CSharp" + " Expert";
Again we will put all these concepts in a working example and will see what the output of this code is. Have a look at Example2.

Example5

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace CSharpTutorial
{
    class Program
    {
      static void Main()
        {
            Console.WriteLine("Using Split functions");
            string content = "I am a CSharp Expert";
            string[] str = content.Split();
            foreach (string word in str)
            {
                Console.Write(word + "|");
            }
            Console.WriteLine();
            content = "I,am,a,CSharp,Expert";
            str = content.Split(',');
            foreach (string word in str)
            {
                Console.Write(word + " ");
            }
            Console.WriteLine("\n==========\n");

            Console.WriteLine("Using Join functions");
            str = "I am a CSharp Expert".Split(' ');
            content = string.Join(",", str);
            Console.WriteLine(content);
            Console.WriteLine("==========\n");

            Console.WriteLine("Using Concatenate functions");
            string name = string.Concat("I ", "am ", " a", " CSharp", " Expert");
            Console.WriteLine(name);

            name = "I " + "am " + " a" + " CSharp" + " Expert";
            Console.WriteLine(name);

            Console.ReadLine();
        }
    }
}

In the main method we have first split the string and then have displayed each element of the string array using foreach loop. Next we have called Join and Concat methods respectively to display their functionality as discussed earlier. The output of this code would be as follows.

Output5

http://imgs.g4estatic.com/c-sharp/strings/output5.png

String Comparison



String comparison is probably the most frequent task that you will do while programming. Whether it is a signup process or shopping cart application, string comparison lays at the heart of such applications. The simplest example of string comparison can be.
Code:

if(name == “Expert”)
{
    Do something
}

Here we are saying that if the string stored in string instance ‘name’ is equal to string literal “Expert”, this statement should evaluate to true otherwise false. This method of comparing two strings is absolutely fine but it has some limitations. First is that this comparison is case sensitive. And secondly, a string might contain characters that do not belong to English language or you application might be running in different cultures, in such scenario the above method is futile. But doesn’t worry, .NET framework provide solution for both of the above problems.

The .NET framework has an ‘Equals’ method that takes two strings to compare along with a third argument that is used to pass some additional information regarding the case and cultural sensitivity. There are three major variations of System.StringComparison enum.
  • InvariantCulture - If this enum is passed as a third argument to the Equals method, the culture insensitivity is enforced and the ordering of the string is done according to standard English en-US as the culture. This should be used if you are developing a standard application for different regions.
  • CurrentCulture - This enum will take the region in which application is being used while comparing the strings. Different cultures have different alphabets and also the ordering of those alphabets might be different to that used in en-US standard. Therefore, if you are developing separate version of applications for different regions, you should pass this enum.
  • Ordinal - Ordinal is the third comparison type. It simply compares and orders the string based on the ASCII/Unicode values. This should be used in scenarios where you want to compare strings based on the ASCII values.
You must be wondering, where is the case insensitivity feature in all of the above mentioned string. The answer is simple; you just have to append the string ‘IgnoreCase’ with these enums to ignore the case. The new values are also actually enums of the String.Comparison type. So, if you want to compare the strings in region independent application and you want to ignore the case as well you can pass the enum ‘’InvariantCultureIgnoreCase”, similarly you can use “CurrentCultureIgnoreCase” and OrdinalIgnoreCase to ignore the case of the string where you want to use current culture and ordinal comparisons respectively.

The concepts explained in String Comparison section have been implemented in Example3. Have a look at Example3 to understand the usage, functionality and variations of the Equals method for string comparison.

Example6

Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial {
    class Program {
        static void Main() {
            string name = "Expert";
            if (name == "EXPERT")
                    Console.WriteLine("The strings are equal");
            else
                Console.WriteLine("The strings are not equal.");

            if (string.Equals(name, "EXPERT",StringComparison.CurrentCultureIgnoreCase))
                Console.WriteLine("The strings are equal");
            else
                Console.WriteLine("The strings are not equal.");

            if (string.Equals(name, "EXPERT", StringComparison.InvariantCultureIgnoreCase))
                Console.WriteLine("The strings are equal");
            else
                Console.WriteLine("The strings are not equal.");

            if (string.Equals(name, "EXPERT", StringComparison.OrdinalIgnoreCase))
                Console.WriteLine("The strings are equal");
            else
                Console.WriteLine("The strings are not equal.");

            Console.ReadLine();
        }
    }
}

The example is simple to understand, we have implemented the Equals method along with its variations to explain the string comparison.

The output of this code is as follows

http://imgs.g4estatic.com/c-sharp/strings/output6.png

This article was the continuation of the part1 of the article on strings. In this article we further explored some string functions. We studied string manipulation, string splitting and joining and finally we discussed how strings can be compared based on case and culture. For more exciting tutorials on C# and .NET keep visiting this site.


All times are GMT +5.5. The time now is 19:45.