String.Format ๋ฉ์๋ (System) | Microsoft Docs (์ด๋ฒ ํ์ด์ง ์์ ์์ ์ฌ์ฉํ String.Format ๋ฉ์๋!)
String.Format ๋ฉ์๋ (System)
์ง์ ๋ ํ์์ ๋ฐ๋ผ ๊ฐ์ฒด์ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ฝ์ ํฉ๋๋ค.Converts the value of objects to strings based on the formats specified and inserts them into another string. String.Format ๋ฉ์๋๋ฅผ ์ฒ์ ์ฌ
docs.microsoft.com
์ธํฐํ์ด์ค๋ฅผ ์์ํ ์ ์๋ ๊ฒ์ ํด๋์ค ๋ฟ์ด ์๋๋ค.
ํด๋์ค, ๊ตฌ์กฐ์ฒด๋ ๋ฌผ๋ก ์ด๊ณ ์ธํฐํ์ด์ค๋ ์ธํฐํ์ด์ค๋ฅผ ์์ํ ์ ์๋ค.
๊ธฐ์กด์ ์ธํฐํ์ด์ค์ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ณ ์ถ์ ๋ ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๋ฉด ๋๋ค.
ํนํ ์๋ ๊ฒฝ์ฐ์ฒ๋ผ ์ธํฐํ์ด์ค๋ฅผ ์์ ํ ์ ์์ ๋๋ ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํด์ผ ํ๋ค.
- ์์ํ๋ ค๋ ์ธํฐํ์ด์ค๊ฐ ์์ค ์ฝ๋๊ฐ ์๋ ์ด์ ๋ธ๋ฆฌ๋ก๋ง ์ ๊ณต๋๋ ๊ฒฝ์ฐ : .NET SDK์์ ์ ๊ณตํ๋ ์ธํฐํ์ด์ค๋ค์ด ๊ทธ ์์ด๋ค. ์ด์ ๋ธ๋ฆฌ ์์ ์๊ธฐ ๋๋ฌธ์ ์ธํฐํ์ด์ค๋ฅผ ์์ ํ ์ ์๋ค. ์ด ์ธํฐํ์ด์ค์ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ณ ์ถ์ผ๋ฉด ์์ํ๋ ์๋ฐ์ ์๋ค.
- ์์ํ๋ ค๋ ์ธํฐํ์ด์ค์ ์์ค ์ฝ๋๋ฅผ ๊ฐ๊ณ ์์ด๋ ์ด๋ฏธ ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ํด๋์ค๋ค์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ :
ํด๋์ค๋ ๋ฐ๋์ ์ธํฐํ์ด์ค์ '๋ชจ๋ ' ๋ฉ์๋์ ํ๋กํผํฐ๋ฅผ ๊ตฌํํด์ผ ํ๋ค.
์ธํฐํ์ด์ค์ ์ฌ์ํ ์์ ์ด๋ผ๋ ์ด๋ฃจ์ด์ง๋ฉด ์ด ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ๊ธฐ์กด ํด๋์ค๋ค์ ์์ค ์ฝ๋๋ฅผ ๋น๋ํ ๋ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
์ด๋ฐ ์ํฉ์์ ๊ธฐ์กด์ ์์ค ์ฝ๋์ ์ํฅ์ ์ฃผ์ง ์๊ณ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ธฐ ์ํด์๋ ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ๋ ํธ์ด ์ข๋ค.
interface ํ์์ธํฐํ์ด์ค : ๋ถ๋ชจ์ธํฐํ์ด์ค
{
//...์ถ๊ฐํ ๋ฉ์๋ ๋ชฉ๋ก
}
ํ์ ์ธํฐํ์ด์ค๋ ๊ธฐ๋ฐ ์ธํฐํ์ด์ค์ ์ ์ธ๋ ๋ชจ๋ ๊ฒ์ ๊ทธ๋๋ก ๋ฌผ๋ ค๋ฐ๋๋ค.
interface ํ์์ธํฐํ์ด์ค : ๋ถ๋ชจ์ธํฐํ์ด์ค
{
//...์ถ๊ฐํ ๋ฉ์๋ ๋ชฉ๋ก
}
ํ์ ์ธํฐํ์ด์ค๋ ๊ธฐ๋ฐ ์ธํฐํ์ด์ค์ ์ ์ธ๋ ๋ชจ๋ ๊ฒ์ ๊ทธ๋๋ก ๋ฌผ๋ ค๋ฐ๋๋ค.
๋ค์์ ILogger ์ธํฐํ์ด์ค๊ฐ ์๋ค๋ฉด...
interface ILogger
{
void WriteLog(string message);
}
๊ทธ๋ฆฌ๊ณ ๋ค์๊ณผ ๊ฐ์ด ILogger ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ์๋ก์ด ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ ๋ค๊ณ ํด๋ณผ ๋,
๋ค์์ IFormattableLogger ์ธํฐํ์ด์ค๋
ILogger์ ์ ์ธ๋ void WriteLog(string messsage)์
void WriteLog(string format, params Object[] args) ๋ฉ์๋ ๋ ๊ฐ๋ฅผ ๊ฐ๊ฒ ๋๋ค.
interface IFormattableLogger : ILogger
{
void WriteLog(string format, parans Object[] args);
}
<์์ >
using System;
namespace DerivedInterface
{
interface ILogger
{
void WriteLog(string message);
}
interface IFormattableLogger : ILogger
{
void WriteLog(string format, params object[] args);
}
class ConsoleLogger2 : IFormattableLogger
{
public void WriteLog(string message)
{
Console.WriteLine("{0} {1}",DateTime.Now.ToLocalTime(),message);
}
public void WriteLog(string format, params object[] args)
{
String message = String.Format(format, args);
Console.WriteLine("{0} {1}",DateTime.Now.ToLocalTime(), message);
}
}
class MainApp
{
static void Main(string[] args)
{
IFormattableLogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat.");
logger.WriteLog("{0}-> {1} ->{2}", 0,1,2);
}
}
}
<์์ ์ถ๋ ฅ ๊ฒฐ๊ณผ>
2022-02-22 ์ค์ 11:58:12 The world is not flat.
2022-02-22 ์ค์ 11:58:12 0-> 1 ->2
<์์ ๋น๋์ >
using System;
using System.IO;
namespace DerivedInterface
{
interface ILogger // ์ธํฐํ์ด์ค!
{
void WriteLog(string message);
}
class ConsoleLogger2 : ILogger
{
public void WriteLog(string message)
{
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(),message);
}
}
class MainApp
{
static void Main(string[] args)
{
ILogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat."); //format = "The world is not flat.", args = object[0]
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:54:52 The world is not flat.
//logger.WriteLog("{0} + {1} = {2}",1,1,2); //args[0]=1, args[1]=1, args[2]=2 //์ปดํ์ผ ์๋ฌ!
}
}
}
<์์ >
using System;
using System.IO;
namespace DerivedInterface
{
interface ILogger // ์ธํฐํ์ด์ค!
{
void WriteLog(string message);
}
interface IFormattableLogger : ILogger //IFormattableLogger๋ ILogger๋ฅผ ์์ํ๋ค.
{
void WriteLog(string format, params Object[] args);
}
class ConsoleLogger2 : IFormattableLogger
{
public void WriteLog(string message)
{
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(),message);
}
public void WriteLog(string format, params Object[] args)
{
String messsage = String.Format(format, args);
Console.WriteLine("{0} {1}",DateTime.Now.ToLocalTime(),messsage);
}
}
class MainApp
{
static void Main(string[] args)
{
IFormattableLogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat."); //format = "The world is not flat.", args = object[0]
logger.WriteLog("{0} + {1} = {2}",1,1,2); //args[0]=1, args[1]=1, args[2]=2
}
}
}
<์์ ์ฃผ์๋ณธ>
using System;
using System.IO;
namespace DerivedInterface
{
interface ILogger // ๊ธฐ๋ฐ ์ธํฐํ์ด์ค
{
void WriteLog(string message);
}
interface IFormattableLogger : ILogger //IFormattableLogger๋ ILogger๋ฅผ ์์ํ๋ค.
{
void WriteLog(string format, params Object[] args);
}
class ConsoleLogger2 : IFormattableLogger
{
public void WriteLog(string message) //๊ธฐ๋ฐ์ธํฐํ์ด์ค์ ๋ฉ์๋๋ ๊ตฌํํด์ฃผ์ด์ผ ํจ
{
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), message);
}
public void WriteLog(string format, params Object[] args) //ํ์์ธํฐํ์ด์ค ๋ฉ์๋ ๊ตฌํ
{
String messsage = String.Format(format, args);
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), messsage);
}
}
class MainApp
{
static void Main(string[] args)
{
IFormattableLogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat."); //format = "The world is not flat.", args = object[0]
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:55:38 The world is not flat.
logger.WriteLog("{0} + {1} = {2}", 1, 1, 2); //format = "{0} + {1} = {2}"
//string.Format ๋ฐํ๋๊ณ ๋ ๋ค message = "1 + 1 = 2"
//args[0]=1, args[1]=1, args[2]=2
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:55:59 1 + 1 = 2
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
2022-02-15 ์ค์ 11:01:14 The world is not flat.
2022-02-15 ์ค์ 11:01:14 1 + 1 = 2
๊ทธ๋ฐ๋ฐ ์ฌ์ค...
๊ตณ์ด ์ธํฐํ์ด์ค๊ฐ ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ง ์์๋ ๊ด์ฐฎ์ ์์ ์์
using System;
using System.IO;
namespace DerivedInterface
{
interface IFormattableLogger
{
void WriteLog(string format, params Object[] args);
}
class ConsoleLogger2 : IFormattableLogger
{
public void WriteLog(string message)
{
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), message);
}
public void WriteLog(string format, params Object[] args)
{
String messsage = String.Format(format, args);
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), messsage);
}
}
class MainApp
{
static void Main(string[] args)
{
IFormattableLogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat."); //format = "The world is not flat.", args = object[0]
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:55:38 The world is not flat.
logger.WriteLog("{0} + {1} = {2}", 1, 1, 2); //format = "{0} + {1} = {2}"
//string.Format ๋ฐํ๋๊ณ ๋ ๋ค message = "1 + 1 = 2"
//args[0]=1, args[1]=1, args[2]=2
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:55:59 1 + 1 = 2
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
2022-02-15 ์ค์ 11:06:44 The world is not flat.
2022-02-15 ์ค์ 11:06:44 1 + 1 = 2
<์ถ๊ฐ ์คํ>
ILogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat.");
์ปดํ์ผ ์๋ฌ -> logger.WriteLog("{0} + {1} = {2}", 1, 1, 2); //format = "{0} + {1} = {2}"
์ปดํ์ผ ์๋ฌ : ์ธ์ 4๊ฐ๋ฅผ ์ฌ์ฉํ๋ 'WriteLog' ๋ฉ์๋์ ๋ํ ์ค๋ฒ๋ก๋๊ฐ ์์ต๋๋ค.
ILogger ์๋ ์ธ์ 4๊ฐ๋ฅผ์ฌ์ฉํ๋ 'WriteLog' ๋ฉ์๋ ์ ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ๋น์ฐํ ๊ฒฐ๊ณผ์ด๋ค.
using System;
using System.IO;
namespace DerivedInterface
{
interface ILogger // ๊ธฐ๋ฐ ์ธํฐํ์ด์ค
{
void WriteLog(string message);
}
interface IFormattableLogger : ILogger //IFormattableLogger๋ ILogger๋ฅผ ์์ํ๋ค.
{
void WriteLog(string format, params Object[] args);
}
class ConsoleLogger2 : IFormattableLogger
{
public void WriteLog(string message) //๊ธฐ๋ฐ์ธํฐํ์ด์ค์ ๋ฉ์๋๋ ๊ตฌํํด์ฃผ์ด์ผ ํจ
{
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), message);
}
public void WriteLog(string format, params Object[] args) //ํ์์ธํฐํ์ด์ค ๋ฉ์๋ ๊ตฌํ
{
String messsage = String.Format(format, args);
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), messsage);
}
}
class MainApp
{
static void Main(string[] args)
{
ILogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat."); //format = "The world is not flat.", args = object[0]
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:55:38 The world is not flat.
logger.WriteLog("{0} + {1} = {2}", 1, 1, 2); //format = "{0} + {1} = {2}"
//string.Format ๋ฐํ๋๊ณ ๋ ๋ค message = "1 + 1 = 2"
//args[0]=1, args[1]=1, args[2]=2
//์ถ๋ ฅ
//2022-02-15 ์ค์ 10:55:59 1 + 1 = 2
}
}
}
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
37. ์ถ์ํด๋์ค : ์ธํฐํ์ด์ค์ ํด๋์ค ์ฌ์ด (0) | 2022.02.22 |
---|---|
35. ์ธํฐํ์ด์ค ๋ค์ค ์์ (0) | 2022.02.15 |
33. ์ธํฐํ์ด์ค(1) (0) | 2022.02.14 |
์ด๊ฒ์ด C#์ด๋ค 7์ฅ ์ฐ์ต๋ฌธ์ ์ฐธ๊ณ (0) | 2022.02.14 |
32. ํํ (0) | 2022.02.14 |