๐Ÿ“šString Format

๐Ÿ“„stirng์—์„œ int๋กœ ๋ณ€ํ™˜

1
2
3
// string -> int
string input = "100";
int number = int.Parse(input);


๐Ÿ“„String Format

๊ฐœ์ฒด, ๋ณ€์ˆ˜ ๋˜๋Š” ์‹์˜ ๊ฐ’์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์— ์‚ฝ์ž…ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

1
2
3
4
5
6
// int -> string
int hp = 90;
int maxHp = 100;

string message = string.Format("HP: {0}, MaxHP: {1}", hp, maxHp);
Console.WriteLine(message);


๐Ÿ“„String Interpolation

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ณด๊ฐ„๋œ ๋ฌธ์ž์—ด๋กœ ์‹๋ณ„ํ•˜๋ ค๋ฉด $ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•œ๋‹ค.

1
2
3
// string interpolation
string message = $"HP: {hp}, MaxHP: {maxHp}";
Console.WriteLine(message);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            // string -> int
            string input = "100";
            int number = int.Parse(input);

            Console.WriteLine(number);

            // int -> string
            int hp = 90;
            int maxHp = 100;

            //string message = string.Format("HP: {0}, MaxHP: {1}", hp, maxHp);
            //Console.WriteLine(message);

            // string interpolation
            string message = $"HP: {hp}, MaxHP: {maxHp}";
            Console.WriteLine(message);
        }
    }



Leave a comment