C# Strings

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

C# Strings:

Strings in C# are sequences of characters, widely used for text manipulation. They are immutable, meaning their values cannot be changed after creation.

String Length:

You can find the length of a string in C# using the Length property of the string, which returns the number of characters in the string.

Example:

string myString = "Hello, C#";
int length = myString.Length; // length will be 9

C# String Concatenation:

String concatenation in C# is the process of combining two or more strings into a single string using the + operator or the string.Concat() method.

Example:

string firstName = "Mohan";
string lastName = "Kumar";
string fullName = firstName + " " + lastName; // fullName will be "John Doe"

Adding Numbers and Strings:

You can concatenate strings with numbers in C# using the + operator, but you should convert numbers to strings explicitly using ToString() method or string interpolation.

Example:

int age = 30;
string message = "I am " + age.ToString() + " years old."; // message will be "I am 30 years old."

C# String Interpolation:

String interpolation is a convenient way to embed expressions and variables directly within a string using the $ symbol and curly braces {}.

Example:

int apples = 5;
string output = $"I have {apples} apples."; // output will be "I have 5 apples."

C# Access Strings:

You can access individual characters of a string by their index using square brackets [].

Example:

string myString = "C# Programming";
char firstChar = myString[0]; // firstChar will be 'C'

Strings - Special Characters:

C# allows you to include special characters in strings using escape sequences, like \n for a newline or \" for a double quote within a string.

Example:

string message = "This is a special\ncharacter: \"";
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this