Strings
Lesson 6Author : Afrixi
Last Updated : February, 2023
In C#, a string is a sequence of characters that represents text. Strings are used extensively in C# programs for tasks such as displaying text on the screen, storing user input, and manipulating text data.
Here are some important things to know about strings in C#:
Strings are reference types: This means that when you declare a string variable, you’re actually creating a reference to a string object in memory. This is in contrast to value types like integers, which are stored directly in memory.
String literals: You can declare a string literal by enclosing the text in double quotes. For example: string str = "Hello, world!";
Concatenation: You can concatenate (or join) strings using the +
operator. For example:
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
String interpolation: You can embed values within a string using string interpolation. This involves enclosing the value in curly braces {}
within a string literal, and prefixing the string with a $
character. For example:
int age = 30;
string message = $"My age is {age}.";
Length: You can determine the length of a string using the Length
property. For example:
string str = "Hello, world!";
int length = str.Length;
Indexing: You can access individual characters in a string using indexing. For example: string str = "Hello, world!"; char firstChar = str[0];
String methods: There are many built-in methods for working with strings in C#, such as ToUpper()
, ToLower()
, Substring()
, IndexOf()
, Replace()
, and Split()
. These methods can be used to perform tasks like converting a string to uppercase, extracting a substring, finding the index of a character, replacing text within a string, and splitting a string into substrings.
Here’s an example of how to use some of these string features in C#:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Declare a string variable
string str = "Hello, world!";
// Concatenate strings
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
// String interpolation
int age = 30;
string message = $"My name is {fullName} and I am {age} years old.";
// Length
int length = str.Length;
// Indexing
char firstChar = str[0];
// String methods
string upperCase = str.ToUpper();
string lowerCase = str.ToLower();
string substring = str.Substring(7, 5);
int index = str.IndexOf("world");
string replaced = str.Replace("world", "C#");
string[] parts = str.Split(",");
// Output values to console
Console.WriteLine(str);
Console.WriteLine(fullName);
Console.WriteLine(message);
Console.WriteLine(length);
Console.WriteLine(firstChar);
Console.WriteLine(upperCase);
Console.WriteLine(lowerCase);
Console.WriteLine(substring);
Console.WriteLine(index);
Console.WriteLine(replaced);
Console.WriteLine(parts[0]);
Console.WriteLine(parts[1]);
Console.ReadKey();
}
}
}
In this example, we’re declaring a string variable str
and initializing it with the value "Hello, world!"
. We’re then demonstrating various string features, such as concatenation, string interpolation, length, indexing, and string methods. We’re outputting the values