본문 바로가기

c# ☃️

[c#] string manipulation (문자열 조작 😵‍💫)

namespace StringManipulation;
class Program
{
    static void Main(string[] args)
    {
        //define few variables

        int age = 7;
        string name = "fred";
        string job = "student";

        // 1. string concatenation
        Console.WriteLine("String Concaenation");
        Console.WriteLine("Hi i'm " + name + " ,i'm " + age+"years old");

        // 2. String formatting
        // string formatting uses index
        Console.WriteLine("String Formattion");
        Console.WriteLine("Hi i'm {0}, i'm {1} years old", name, age);

        // 3. String interpolation
        // variables like this {var}
        Console.WriteLine("String Interpolation");
        Console.WriteLine($"Hi i'm {name}, i'm {age} years old. i'm a {job}");


        // 4. Verbatim strings.
        // literally and ignore any spaces and escape characters like \n
        Console.WriteLine("Verbatim strings");
        Console.WriteLine(@"hi
        i'm who");

        //instead of using \\ to write file paths we can use verbatim string to make it easier
        Console.WriteLine(@"/Users/bdins/ChatGPT_logo.svg.png");

        //with verbatim strings even vaild escape characters won't work
        Console.WriteLine(@"huk! \n u have no powers here");
        Console.WriteLine("huk! \n u have powers here");

    }
}

1. string concatenation 결과


2. string formatting 결과


3.string interpolation 결과


4.verbatim strings 결과