๐stirng์์ int๋ก ๋ณํ
1
2
3
| // string -> int
string input = "100";
int number = int.Parse(input);
|
๊ฐ์ฒด, ๋ณ์ ๋๋ ์์ ๊ฐ์ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ฝ์
ํด์ผ ํ๋ ๊ฒฝ์ฐ ๋ฅผ ์ฌ์ฉํ๋ค.
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