C# Substrings

A substring, as the name indicates, is a part of a string. This part of substring can be from the start, middle, or the end of the given string. A substring() function in C sharp is a built-in function of string, that is used to extract a specified portion of a string by mentioning the starting point. A substring function is used mainly in two ways. One is with a start index and the second one is with the start index and the length.

Syntax

String str = substring(startindex)

The second parameter ‘length’ can be added for the specific endpoint of the substring.

Implementation of Substring

Example 1: Substring with a Start Index

This is the program of having a substring from a string in C sharp. A substring is a part of the string. So for the implementation, we need to have a string from which a substring is created. A substring function is a built-in function of a string and is linked with the string library. But in this program, it is used directly, instead of using the <string> header file, used in C++ programming language. Here, a ‘system’ library is used to contain the information regarding system and namespaces that include all the classes and structures we use in the source code.

String str = "I am Aqsayasin";

This string will be displayed through the console.writeline statement when the program is executed. As we are working on the type of a substring where we mention the start index for a string so that the substring is created from that particular point. There is no ending point declaration. So the string is created from the start index till the last character of the string. The basic syntax of this function is mentioned below:

string Substring(startIndex);

The function has a single argument. The argument is for the index of start for the new substring. The data type is an integer for argument, and as a whole function, the return type is a string value.

We will apply this function to the string we have used. For instance, we want to create a substring from the character present at the 5th index, so we will use ‘5’ as the parameter. The function is called through the string object.

Str. Substring(5);

Similarly, for the 9th index, 9 will be used.

After applying the function, we will get ‘Aqsayasin’. Now we will execute the above program in the terminal through a compiler ‘MCS’ that is needed to compile the code and ‘mono’ will execute the program.

$ MCS file.cs

$ mono file.exe

On execution, you will see that at the 5th index, the starting index from 0, ‘a’ is present so the substring hereafter is created. Similarly, at index 9, ‘y’ is present so from this character till the end, another substring will be created.

Example 2: Substring () Function with Length

The substring function () is also used to create a substring of a specified length. So this function takes two parameters with it. The syntax for this function is;

String Substring(startIndex, length);

Both parameters are of integer data type. The first parameter is for the first index of the new substring (startindex), whereas the ‘length’ parameter provides the number of characters that will be present in the substring.

A value that is returned is of a string data type. To apply this function, we will declare a string at the start. We have again used two different parameters to elaborate the functionality with different examples. The first function contains a 0 starting index with a ‘6’ length.

Str. Substring (0, 6);

This means that the starting index of the substring is 0 and it will cover the next 6 characters with it. Similarly, for the second option, the startindex is ‘7’ means the starting point will be at the 7th index and it will have a length of 7 characters. This is how we can take a middle, start, or ending part of the string depending on the startindex and the length we provide for the sub-string.

Str. Substring (7, 7);

We will execute the above program to see the resultant values.

The first sub-string starts from 0 index and 6 letters are present so the result is ‘I am a’. Similarly, the 7th index contains ‘c’ and after counting the index from the 7th index to onwards till the 7 lengths of characters, we will get ‘content’.

Example 3: Substring Function by Mentioning a Specific Character

Not only by providing the startindex and the length we obtain the substring. By mentioning a specific character, a substring is also obtained. So in this example, a function of a substring with another built-in function is used. The substring function will contain the start index and that function in the parameter. This function will identify the index in the string of the specific character.

As we do not have mentioned the length, only the specific character is mentioned, so we first need to identify the index number of the character. The character can be any symbol, alphabet, or numeric value. Here we have used a colon in the string. A string is first declared. Then we will use the function of the substring.

String result = text.substring (0, text.IndexOf (':'));

The return type of the inner ‘text. index of()’ will be of integer data type, as it will return only the index number that is an integer. Whereas the return type of the overall function will be a string.

On execution, we will get ‘c#’.

This function will work in such a way that when the index of the character is identified, then the substring function starts its work, as the index number is the last index. So from the start index that is 0, the substring is created till the specified character.

Exceptions

There are chances of occurrence of errors while executing the program for the substrings.

  • Case 1: If you have a start index only as a parameter, then the value of the start index should lie between 0 and the length of the current instance. Otherwise, if the value goes beyond this limit, then it will raise an error ‘Argumentoutofrangeexpression’.
  • Case 2: If two parameters, the start index and the length are the parameters for the substring function, then the limit should be followed for the startindex as described above. If the startindex and the length show the position out of the instance, then the error occurs because of the out-of-range argument.

Conclusion

A substring() function is only for the strings. This function returns a specific portion of the string that depends on the value of parameters you provide to the function. In C sharp, we can use the main two approaches for implementing the substring function: one is with the start index value, and the second is with the length as an additional parameter. Another way can use for the substring is by mentioning a specified character in the string. But one should remember that the character must be present inside the string, otherwise there are chances of exceptions to be raised. All of these approaches are explained via elementary examples.



from https://ift.tt/UcW0xZ2

Post a Comment

0 Comments