0% found this document useful (0 votes)
0 views

6. C# Strings

The document provides an overview of strings in C#, detailing their definition, properties, and methods for manipulation such as length, concatenation, and interpolation. It includes examples of using string methods like ToUpper(), ToLower(), and Substring(), as well as how to access characters by index. Additionally, it explains the use of escape characters for special characters within strings.

Uploaded by

sohanxt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

6. C# Strings

The document provides an overview of strings in C#, detailing their definition, properties, and methods for manipulation such as length, concatenation, and interpolation. It includes examples of using string methods like ToUpper(), ToLower(), and Substring(), as well as how to access characters by index. Additionally, it explains the use of escape characters for special characters within strings.

Uploaded by

sohanxt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

C# Strings

Sumaiya Tanjil Khan


Lecturer
Department of CSE
Uttara University
Strings are used for storing text.

A string variable contains a collection of characters surrounded by double


quotes.

Example:

string greeting = "Nice to meet you!";


String Length
A string in C# is actually an object, which contain properties and methods
that can perform certain operations on strings. For example, the length of
a string can be found with the Length property:
Example:
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Console.WriteLine("The length of the txt string is: " + txt.Length);


● There are many string methods available, for example
ToUpper() and ToLower(), which returns a copy of the string
converted to uppercase or lowercase.
● string txt = "Hello World";
● Console.WriteLine(txt.ToUpper()); //HELLO WORLD
● Console.WriteLine(txt); //Hello World
● Console.WriteLine(txt.ToLower()); //hello world
String Concatenation
The + operator can be used between strings to combine them. This is
called concatenation:
string firstName = "Will ";
string lastName = "Brown";
string name = firstName + lastName;

Console.WriteLine(name); //Will Brown


You can also use the string.Concat() method to concatenate two strings:
string firstName = "Will ";
string lastName = "Brown";
string name = string.Concat(firstName, lastName);

Console.WriteLine(name);
C# String Interpolation
Another option of string concatenation, is string interpolation, which
substitutes values of variables into placeholders in a string.
string firstName = "John";
string lastName = "Doe";

Console.WriteLine($"My full name is: {firstName} {lastName}");


C# Access Strings
Access the characters in a string by referring to its index number
inside square brackets [].
This example prints the first character in myString:
string myString = "Hello";

Console.WriteLine(myString[2]);
Another useful method is Substring(), which extracts the characters from a
string, starting from the specified character position/index, and returns a new
string.
This method is often used together with IndexOf() to get the specific character
position.
string name = "John Doe";
int charPos = name.IndexOf("D");//5
int substring_length=2;
string lastName = name.Substring(charPos,substring_length); //Do

Console.WriteLine(lastName); //Do
Strings - Special Characters
string txt = "We are \"Batch 60B\" of UU.";

The solution to avoid this problem, is to use the backslash escape character.

The backslash (\) escape character turns special characters into string characters

You might also like