Syntax:
In this syntax, we see that we declare an integer “int” with any name of our choice and assign the string length to this integer value by using our “str_name.Length”.
Example # 1: Calculating string length using the Built-in method
In the given example, we are going to calculate the length of the string which is given in the C# program in Ubuntu 20.04. You must write the given code on your text editor. We are performing this example on Ubuntu 20.04 text editor. You must have saved this file after completing this code with the extension of “.cs”.
In this given code, we are importing the “using System” library. You can define many resources in a single line with the help of the “using” statement. The “using System” is used in the C# program for accessing functions and methods that are used in this C# program. Now, we are using the namespace in the next line. The namespace is named “stringLength” in this code. Then, we are declaring a class with the name “CSharp” here. Inside this class, we invoked a function which is the main function of the C# program.
Here, “static void Main(String[ ] args)” is the main method of this program. This “main” function is static and “(String [ ] args) is used here for the arguments in this “main” function. The “main” function is used for the execution of the code which is written inside the “main” method. Inside the curly braces of this “main” method, we are declaring a string with a name “S” and then assign a string character to that string. Here in this code, we assign “aeiou” to this string. We want to calculate the length of this given string. For this, we use the syntax of string length which we described above.
We are declaring integers as “int” with a name “leng” and assign “string_name. Length” to this integer. Now “S.Length” is assigned to “int leng” in this code, which will calculate the character including spaces in this string. Here, we have no space in this string. After all this, it’s the time to print or display the string length on the screen and for this, we are using “Console.WriteLine” which prints all data which is written inside it. We are displaying the string length here with the help of this “Console.WriteLine”.
For displaying the output in Ubuntu 20.04 terminal, we have the commands which are given here. Now, have a look at these commands, so that you can easily get the output using these commands. First, we are using the “mcs” command and “strleng” is the filename here with the extension of “.cs”. And after this command, we are using the “mono” command with a filename having an extension of “.exe”. Just remember one thing, when using the “mcs” command, you must use the “.cs” extension with the filename, and with the “mono” command, you must use the “.exe” extension with the filename.
In this output, you can see it displays the length of the string which we give in the code. As the string in this code is “aeiou” and these are 5 characters so this program returns the length of the string as “5”.
Example # 2: Calculating String Length of the String that the User Entered by Using the Built-in Function
Here, we have another example in which we are getting the string from the user and then calculating the string length using the same method as described in the above example and displaying the length of the string.
We import the library of the C# program “using System”. It will access the functions and also the methods that are required in this C# code. After this, we have a namespace having the name “stringLength”. The “namespace” is used for declaring the scope that encompasses a group of items that are connected.
Now, we are going to create a class with the name “CSharp” in this C# code. We also have a “main” method in this code which is “static void Main(String [ ] args)”. It is an important part of this code. All the code is written inside the curly braces of the main method. We have “Console.WriteLine” in this “main” method which is used for printing the text on the screen. Here, we are displaying the message to the user to enter the string so that the user enters the string that he wants. For getting the input from the user, we use “Console.ReadLine”.
First, we declare and initialize a string with the name “str1” and assign “Console.ReadLine()” to this string, so that all the strings that the user enters will store in this string “str1”. After this, we use the string.Length method for finding the length of the string that the user entered. We declare and initialize an integer “length” and use “str1.Length” for calculating characters and spaces in the string. This string length is stored in this “length” of integer data type. Now, we print this length of users’ string using the “Console.WriteLine” method.
The output of the above C# code is here in the image. We use the same commands as we were using in our previous example. Let’s see in this image below how it will print the length of the string that the user enters.
We see that the user enters the string “My first program of CSharp” string length”. And it displays the length of the string as “40” as it counts the characters and also the spaces and returns the value of this string.
Example # 3: Calculating String Length Without Using the Built-in Method
Now, we are calculating the string length without using any built-in method in the C# programming in Ubuntu 20.04.
The first line of the code is “using System” which we have discussed in detail in our previous example. Then a namespace with the name “LengthOfString”. After this, the class is created with the name “Example”. You can also create the class with the name of your choice. Then, the “main” function is invoked here in this class. Inside the “main” method, we declare a string named “words”. Now, we display the message on the screen for the user to enter the string.
For displaying the message, we are using “Console.WriteLine”. By using “words = Console.ReadLine()”, all the text or string that the user enters is stored in “words” which is declared as the string data type. We declare and initialize an integer “a” in the next line. After this, we use the “foreach” loop, this loop gets the characters with the help of “char c” in “words” and then “a++” increments the value of “a” each time the character is entered by the user. Now, we have “Console.WriteLine” for printing the length of the string which we have discussed in our previous examples.
We get the output of the code which counts the characters along with spaces and displays the string length without using any built-in method.
Conclusion
In this guide, we learned about the string length in the C# program in Ubuntu 20.04. We have explained this concept in detail and demonstrated different examples for better understanding. We also show how to find the length of the string that is entered by the user. We can find the length by using the built-in function and also without using the built-in function in this guide. These examples will help you in finding the length of any string in the C# program. Also, we explained how to run C# code and get output in Ubuntu 20.04. I hope this guide will provide you with great benefits and you will easily find string length using these methods.