C# Strings

Strings are used to store text.

A string variable consists of a set of characters surrounded by double quotes:

Example
            
                string 
                "greeting" 
                = 
                "Hello"
                ;
            
        

String Length

A string in C# is actually an object, containing structures and modes that can perform certain functions in a character unit. For example, the length of a character unit can also be found in the length feature:

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

Other Methods

There are many string methods available, for example ToUpper() and ToLower(), which return a copy of the character unit converted to uppercase or lowercase letters:

Example
            
                string 
                "txt" 
                = 
                "Hello World"
                ;
                "Console"
                .
                WriteLine
                (
                "txt"
                .
                ToUpper
                (
                )
                )
                ;   // Outputs "HELLO WORLD"
                "Console"
                .
                WriteLine
                (
                "txt"
                .
                ToLower
                (
                )
                )
                ;   // Outputs "hello world"
            
        

String Concatenation

Operator + can be used between wires to connect them. This is called concatenation:

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

You can also use the string.Concat() method to combine two character units:

Example
            
                string 
                firstName 
                = 
                "John "
                ;
                string 
                "lastName" 
                = 
                "Doe"
                ;
                string 
                "name" 
                = 
                string
                .
                Concat
                (
                "firstName"
                , "lastName"
                )
                ;
                "Console"
                .
                WriteLine
                (
                "name"
                )
                ;
            
        

String Interpolation

Another option for merging a string, is string interpolation, which converts variable values ​​into representations in a character unit. Note that you do not have to worry about spaces, such as merging:

Example
            
                string 
                "firstName" 
                = 
                "John"
                ;
                string 
                "lastName" 
                = 
                "Doe"
                ;
                string 
                "name" 
                = 
                "$"
                "My full name is: {firstName} {lastName}"
                ;
                "Console"
                .
                WriteLine
                (
                "name"
                )
                ;
            
        

Access Strings

You can access the letters in a series by referring to its reference number inside the square brackets [].

This example prints the first character in myString:

Example
            
                string 
                "myString" 
                = 
                "Hello"
                ;
                "Console"
                .
                WriteLine
                (
                "myString"
                [
                0
                ]
                )
                ;  // Outputs "H"
            
        

This example prints the second character(1) in myString:

Example
            
                string 
                "myString" 
                = 
                "Hello"
                ;
                "Console"
                .
                WriteLine
                (
                "myString"
                [
                1
                ]
                )
                ;  // Outputs "e"
            
        

You can also find the location of a specific character in a string, using the IndexOf() method:

Example
            
                string 
                "myString" 
                = 
                "Hello"
                ;
                "Console"
                .
                WriteLine
                (
                "myString"
                .
                IndexOf
                (
                "e"
                )
                )
                ;  // Outputs "1"
            

Another useful method is Substring(), which releases characters from a string, starts at the specified character / index location, and then returns a new string. This method is often used with IndexOf() to locate a specific location of a character:

Example
            
                // Full name
                string 
                "name" 
                = 
                "John Doe"
                ;
            
                // Location of the letter D
                int 
                "charPos" 
                = 
                "name"
                .
                IndexOf
                (
                "D"
                )
                ;
            
                // Get last name
                
                string 
                "lastName" 
                =
                "name"
                .
                Substring
                (
                "charPos"
                )
                ;
            
                // Print the result
                "Console"
                .
                WriteLine
                (
                "lastName"
                )
                ;
            
        
Special Characters

Because the character units have to be written between quotes, C# will not understand this string correctly, and will produce an error:

            
                string 
                "txt" 
                = 
                "We are the so-called "
                "Vikings"
                " from the north."
                ;
            
        

The solution to avoiding this problem is to use a backslash escape character.

The backslash (\) converts special characters into bullet unit characters:

Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash

The sequence \" inserts a double quote in a string:

Example
            
                string 
                "txt" 
                = 
                "We are the so-called \"Vikings\" from the north."
                ;
            
        

The sequence \' inserts a single quote in a string:

Example
            
                string 
                "txt" 
                = 
                "It\'s alright."
                ;
            
        

The sequence \\ inserts a single backslash in a string:

Example
            
                string 
                "txt" 
                = 
                "The character \\ is called backslash."
                ;
            
        

Other useful escape characters in C# are:

Code Result
\n New Line
\t Tab
\b Backspace

Adding Numbers and Strings

If you add two numbers, the result will be a number:

Example
            
                int 
                "x" 
                = 
                10
                ;
                int 
                "y" 
                = 
                20
                ;
                int 
                "z" 
                = 
                "x" 
                +
                "y"
                ;  // z will be 30 (an integer/number)
            
        

If you add two strings, the result will be a combination of characters:

Example
            
                string 
                "x" 
                = 
                "10"
                ;
                string 
                "y" 
                = 
                "20"
                ;
                string 
                "z" 
                = 
                "x" 
                + 
                "y"
                ;  // z will be 1020 (a string)