C# Keywords Tutorial Part 80: string

C# Keywords Tutorial Part 80: string

C# is a popular programming language used for developing various types of applications, including web applications, desktop applications, games, and mobile applications. One of the most commonly used data types in C# is the string data type. In this blog post, we will discuss the string data type in C#, its properties, and some code examples.

What is a String in C#?

A string is a sequence of characters that represent text. In C#, the string data type is represented by the “string” keyword. The string keyword is used to declare a variable that can store a string of characters.

Example:

string myString = "Hello World!";

In this example, we have declared a variable “myString” of type string and assigned it the value “Hello World!”.

Properties of String in C#

There are several properties of a string in C# that can be used to manipulate the string. Let’s discuss some of these properties.

  1. Length Property:

The Length property is used to get the length of the string. It returns the number of characters in the string.

Example:

string myString = "Hello World!";
int stringLength = myString.Length;

In this example, the “stringLength” variable will have a value of 12 because “Hello World!” has 12 characters.

  1. IndexOf Property:

The IndexOf property is used to get the index of a specific character or substring in the string. It returns the index of the first occurrence of the specified character or substring.

Example:

string myString = "Hello World!";
int index = myString.IndexOf("World");

In this example, the “index” variable will have a value of 6 because the substring “World” starts at index 6 in the string “Hello World!”.

  1. Substring Property:

The Substring property is used to get a substring from the string. It returns a new string that consists of a specified number of characters from the original string.

Example:

string myString = "Hello World!";
string subString = myString.Substring(6, 5);

In this example, the “subString” variable will have a value of “World” because we are getting a substring starting from index 6 with a length of 5 characters.

Code Examples:

Let’s take a look at some code examples that demonstrate the use of string in C#.

Example 1: Concatenating Strings

string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName);

In this example, we are concatenating two strings “firstName” and “lastName” with a space in between them to get the “fullName”.

Output:

John Doe

Example 2: Reversing a String

string myString = "Hello World!";
char[] charArray = myString.ToCharArray();
Array.Reverse(charArray);
string reversedString = new string(charArray);
Console.WriteLine(reversedString);

In this example, we are converting the string “myString” to a character array, reversing the array using the “Reverse” method of the “Array” class, and then creating a new string from the reversed character array.

Output:

!dlroW olleH

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *