본문 바로가기
C#

String interpolation

by 사무실 꿀벌 2021. 2. 19.
반응형
  • String Interpolation
string afriend = "Bill";
string firstFriend = "Maria";
string secondFriend = "Sage";

Console.WriteLine("Hello " + afriend);
// result :  Hello Bill

Console.WriteLine($"Hello world!{afriend}");
// result : Hello world!Bill

Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");
// result : My friends are Maria and Sage

Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");
// result : The name Maria has 5 letters.

Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");
// result : The name Sage has 4 letters.
  • Trim
string greeting = "      Hello World!       ";
Console.WriteLine($"[{greeting}]");
// result : [      Hello World!       ]

string trimmedGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimmedGreeting}]");
// result : [Hello World!       ]

trimmedGreeting = greeting.TrimEnd();
Console.WriteLine($"[{trimmedGreeting}]");
// result : [      Hello World!]

trimmedGreeting = greeting.Trim();
Console.WriteLine($"[{trimmedGreeting}]");
// result : [Hello World!]
  • Replace
string sayHello = "Hello World!";
Console.WriteLine(sayHello);
// result : Hello World!

sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
// result : Greetings World!
  • Contains
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
// result : True

Console.WriteLine(songLyrics.Contains("greetings"));
// result : False

 

반응형

'C#' 카테고리의 다른 글

OpenFileDialog get Path and FileName  (0) 2021.03.09
C# Start "Hello World!  (0) 2020.11.25
C# Override  (0) 2020.07.15