string ํ์์ ๊ทธ์ ๋ฌธ์์ด์ ๋ด๋ ์ญํ ์ ํ ๋ฟ ์๋๋ผ,
๋ฌธ์์ด์ ๊ฐ๊ณตํ๊ธฐ ์ํ ๋ค์ํ ๊ธฐ๋ฅ๋ ํจ๊ป ์ ๊ณตํ๋ค.
<๋ชฉ์ฐจ>
(1) ๋ฌธ์์ด ์์์ ์ฐพ๊ธฐ : ๋ฌธ์์ด ๋ด์ ์ด๋ ๋ถ๋ถ์ ๊ฐ๊ณตํ ๊ฒ์ธ๊ฐ
(2) ๋ฌธ์์ด ๋ณํํ๊ธฐ : ๋ฌธ์์ด ์ค๊ฐ์ ์ฝ์ , ์ญ์ , ๋์๋ฌธ์ ๋ณํ, ๋ฌธ์์ด ์/๋ค ๊ณต๋ฐฑ์ ๊ฑฐ(๊ณต๋ฐฑ์ ๊ฑฐ ๋ฉ์๋ ์ค์!)
(3) ๋ฌธ์์ด ๋ถํ ํ๊ธฐ : ๋ฌธ์์ด ์๋ฅด๊ธฐ, ๋ฌธ์์ด ๋ถ๋ฆฌํ๊ธฐ
(4) ๋ฌธ์์ด ์์๋ง์ถ๊ธฐ : ๋ฌธ์์ด์ด ์ผ์ ํ ํ๊ณผ ๋ชจ์์ ๊ฐ์ถ๋๋ก ๋ง๋ค๊ธฐ
(5) ์ผ์ชฝ/ ์ค๋ฅธ์ชฝ ๋ง์ถค
(6) ์ซ์ ์์ํ
(7) ๋ ์ง ๋ฐ ์๊ฐ ์์ํ
(8) ๋ฌธ์์ด ๋ณด๊ฐ
(1) ๋ฌธ์์ด ์์์ ์ฐพ๊ธฐ
๋ฉ์๋ | ์ค๋ช |
IndexOf() | ํน์ ๋ฌธ์์ด์ ์์น๋ฅผ ์ฐพ๋๋ค. |
LastIndexOf() | ํน์ ๋ฌธ์์ด์ ์์น๋ฅผ ๋ค์์๋ถํฐ ์ฐพ๋๋ค. |
StartsWith() | ํ์ฌ ๋ฌธ์์ด์ด ํน์ ๋ฌธ์์ด๋ก ์์ํ๋์ง๋ฅผ ํ๊ฐํ๋ค. |
EndsWith() | ํ์ฌ ๋ฌธ์์ด์ด ํน์ ๋ฌธ์์ด๋ก ๋๋๋์ง๋ฅผ ํ๊ฐํ๋ค. |
Contains() | ํน์ ๋ฌธ์์ด์ ํฌํจํ๋์ง ํ๊ฐํ๋ค. |
Replace() | ํน์ ๋ฌธ์์ด์ด ๋ค๋ฅธ ์ง์ ๋ ๋ฌธ์์ด๋ก ๋ชจ๋ ๋ฐ๋ ์ ๋ฌธ์์ด์ ๋ฐํํ๋ค. |
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string greeting = "Good Morning";
//IndexOf()
WriteLine($"IndexOf 'Good' : {greeting.IndexOf("Good")}"); //IndexOf 'Good' : 0
WriteLine($"IndexOf 'o' : {greeting.IndexOf("o")}"); //IndexOf 'o' : 1
WriteLine($"IndexOf 'z' : {greeting.IndexOf("z")}"); //IndexOf 'o' : 6
//LastIndexOf()
WriteLine($"LastIndexOf 'Good' : {greeting.LastIndexOf("Good")}"); //LastIndexOf 'Good' : 0
WriteLine($"LastIndexOf 'o' : {greeting.LastIndexOf("o")}"); //LastIndexOf 'o' : 6
WriteLine($"LastIndexOf 'z' : {greeting.LastIndexOf("z")}"); //LastIndexOf 'z' : -1
//StartsWith()
WriteLine($"StartsWith 'Good' : {greeting.StartsWith("Good")}"); //StartsWith 'Good' : True
WriteLine($"StartsWith 'Morning' : {greeting.StartsWith("Morning")}"); //StartsWith 'Morning' : False
//EndsWith()
WriteLine($"EndsWith 'Good' : {greeting.EndsWith("Good")}"); //EndsWith 'Good' : False
WriteLine($"EndsWith 'Morning' : {greeting.EndsWith("Morning")}"); //EndsWith 'Morning' : True
//Contains()
WriteLine($"Contains 'Morning' : {greeting.Contains("Morning")}"); //Contains 'Morning' : True
WriteLine($"Contains 'Evening' : {greeting.Contains("Evening")}"); //Contains 'Evening' : False
//Replace()
WriteLine($"Replaced 'Morning to Evening' : {greeting.Replace("Morning", "Evening")}"); //Replaced 'Morning to Evening' : Good Evening
}
}
}
(2) ๋ฌธ์์ด ๋ณํํ๊ธฐ
๋ฉ์๋ | ์ค๋ช |
ToLower() | ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก |
ToUpper() | ์๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก |
Insert() | ์ง์ ๋ ์์น์ ํน์ ๋ฌธ์์ด ์ฝ์ |
Remove() | ์ง์ ๋ ์์น๋ก๋ถํฐ ์ง์ ๋ ์๋งํผ ๋ฌธ์ ์ญ์ |
Trim() | ํ์ฌ ๋ฌธ์์ด์ ์/๋ค์ ์๋ ๊ณต๋ฐฑ์ ์ญ์ |
TrimStart() | ํ์ฌ ๋ฌธ์์ด์ ์์ ์๋ ๊ณต๋ฐฑ์ ์ญ์ |
TrimEnd() | ํ์ฌ ๋ฌธ์์ด์ ๋ค์ ์๋ ๊ณต๋ฐฑ์ ์ญ์ |
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string str = " NoSpace ";
WriteLine($"ToLower():{str.ToLower()}"); //ToLower(): nospace
WriteLine($"ToUpper():{str.ToUpper()}"); //ToUpper(): NOSPACE
WriteLine($"Insert():{str.Insert(3," lovely ")}"); //Insert(): No lovely Space
WriteLine($"Remove():{str.Remove(3,2)}"); //Remove(): Noace
WriteLine($"Trim():{str.Trim()}"); //Trim():NoSpace
WriteLine($"TrimStart():{str.TrimStart()}"); //TrimStart():NoSpace
WriteLine($"TrimEnd():{str.TrimEnd()}"); //TrimEnd(): NoSpace
}
}
}
(3) ๋ฌธ์์ด ๋ถํ ํ๊ธฐ
๋ฉ์๋ | ์ค๋ช |
Split() | ์ง์ ๋ ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ํ์ฌ ๋ฌธ์์ด์ ๋ถ๋ฆฌํ ๋ค์ ๋ถ๋ฆฌํ ๋ฌธ์์ด์ ๋ฐฐ์ด์ ๋ฐํํ๋ค |
SubString() | ์ง์ ๋ ์์น๋ก๋ถํฐ ์ง์ ๋ ์๋งํผ์ ๋ฌธ์๋ก ์ด๋ฃจ์ด์ง ์ ๋ฌธ์์ด์ ๋ฐํํ๋ค |
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string greeting = "Good-Morning.";
string greet = "Hi Hello Bye";
WriteLine(greeting.Substring(0,5)); //Good-
WriteLine(greeting.Substring(5)); //Morning.
string[] arr = greet.Split(new string[] {" "},StringSplitOptions.None);
WriteLine($"Word Count: {arr.Length}"); //Word Count: 3
foreach (string element in arr)
WriteLine($"{element}");
/*
Hi
Hello
Bye
*/
}
}
}
(4) ๋ฌธ์์ด ์์๋ง์ถ๊ธฐ : ๋ฌธ์์ด ์์ํ์ ์ฌ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ ๋ ๊ฐ์ง,
์ฒซ๋ฒ์งธ๋ string ํ์์ Format() ๋ฉ์๋์ด๊ณ , ๋ ๋ค๋ฅธ ํ๋๋ ๋ฌธ์์ด ๋ณด๊ฐ
๋ ๋ฐฉ๋ฒ ๋ชจ๋ ๋ฌธ์์ด ํ์ ๋๊ณ , ๊ทธ ํ์ ์ด์ฉํด์ ์์ํ๋ ์๋ก์ด ๋ฌธ์์ด์ ๋ง๋ค์ด๋ธ๋ค๋ ๊ณตํต์ ์ด ์๋ค.
WriteLine("์ ๋ชฉ : {0}","์ด๊ฒ์ด C#์ด๋ค."); -> ์์ ํญ๋ชฉ์ ์ฒจ์๋ง ์ด์ฉํ ๊ฒฝ์ฐ
๋ฌธ์์ด ํ์ ์ ๋ ฅํ๋ {0}, {1} .. ๋ฅผ ์ผ์ปซ์ด "์์ ํญ๋ชฉ"์ด๋ผ๊ณ ํ๋ค.
์ด ์์ ํญ๋ชฉ์์ ์ถ๊ฐ ์ต์ ์ ์ ๋ ฅํจ์ผ๋ก์จ ๋ ๋ค์ํ ์์ํ๋ฅผ ์ํํ ์ ์๋ค. '
{์ฒจ์, ๋ง์ถค : ์์ ๋ฌธ์์ด}
์ฒจ์: ์์ ํญ๋ชฉ์ ์ฒจ์
๋ง์ถค: ์ผ์ชฝ/์ค๋ฅธ์ชฝ ๋ง์ถค
์์ ๋ฌธ์์ด: ๋ณํ ์์ ์ง์ ๋ฌธ์์ด
string result = string.Format("{0,-10}DEF","ABC");
WriteLine(result); //ABC DEF
result = string.Format("{0,10}DEF", "ABC");
WriteLine(result); // ABCDEF
์์ ๋ฌธ์์ด์ {0}์ -10์ด๋ผ๋ ๋ง์ถค ์ต์ ์ ์ ๋ ฅํด์ฃผ๋ฉด
result๋ "ABC DEF"๊ฐ ๋๋ค. "DEF"์์ ๋ฌธ์ 10๊ฐ๊ฐ ๋ค์ด๊ฐ ๊ณต๊ฐ์ ๋ง๋ค๊ณ ์(์ผ์ชฝ)์์๋ถํฐ "ABC"๋ฅผ ์ฑ์๋ฃ์
๋ค๋ถํฐ ์ฑ์ฐ๋ ์์ํ๋ 10์นธ์ ๊ณต๊ฐ์ ๋ง๋ค๊ณ
์ผ์ชฝ๋ถํฐ ์ฑ์ธ ๋๋ -10์ ์ ๋ ฅํ์ง๋ง,
์ค๋ฅธ์ชฝ๋ถํฐ ์ฑ์ธ ๋๋ - ๊ธฐํธ ์์ด 10๋ง ์ ๋ ฅํ๋ฉด ๋๋ค.
๋ฌธ์์ด ์์์ ์ผ์ชฝ/์ค๋ฅธ์ชฝ์ผ๋ก ๋ง์ถ๋ ํ๋ก๊ทธ๋จ์ด ์ฌ๋ฌ ๊ฐ์ ํญ๋ชฉ์ ๊ฐ์ง๋ฐํ๊ฒ ์ถ๋ ฅํ ํ์๊ฐ ์์ ๋ ํนํ ์ ์ฉ.
-1- ์ผ์ชฝ/ ์ค๋ฅธ์ชฝ ๋ง์ถค
string fmt = "{0,-20}{1,-15}{2,30}";
{์๋ฒ,(๋ถํธ:์or๋ค=์ผ์ชฝ/์ค๋ฅธ์ชฝ ๋ง์ถค)์นธ์ ๊ฐฏ์}
using System;
using System.Collections.Generic;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string fmt = "{0,-20}{1,-15}{2,30}";
WriteLine(fmt, "Gamer", "Author", "Title");
WriteLine(fmt, "Player", "Buthor", "Iron Man");
WriteLine(fmt, "Enemy", "Cuthor", "This is C#");
WriteLine(fmt, "GameManager", "Duthor", "The C Programming Language");
}
}
}
using System;
using System.Collections.Generic;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string fmt = "{0,20}{1,15}{2,-30}";
WriteLine(fmt, "Gamer", "Author", "Title");
WriteLine(fmt, "Player", "Buthor", "Iron Man");
WriteLine(fmt, "Enemy", "Cuthor", "This is C#");
WriteLine(fmt, "GameManager", "Duthor", "The C Programming Language");
}
}
}
-2- ๋ ์ง ๋ฐ ์๊ฐ ์์ํ
๋ ์ง์ ์๊ฐ์ ํํํ๊ธฐ ์ํด์๋ DateTime ํด๋์ค๊ฐ ํ์ํ๋ค.
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
DateTime dt = new DateTime(2022, 02, 07, 05, 41, 01);
WriteLine($"{dt}");
}
}
}
์ถ๋ ฅ : ๊ตญ๊ฐ ๋ฐ ์ง์ญ ์ค์ ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฒฐ๊ณผ ์ถ๋ ฅ
2022-02-07 ์ค์ 5:41:01
using System;
using System.Text;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
DateTime dt = new DateTime(2022, 02, 07, 17, 48, 01);
//12์๊ฐ ํ์
WriteLine("12์๊ฐ ํ์{0:yyyy-MM-dd tt hh:mm:ss (ddd)}",dt);
//24์๊ฐ ํ์
WriteLine("24์๊ฐ ํ์{0:yyyy-MM-dd tt HH:mm:ss (ddd)}", dt);
WriteLine("24์๊ฐ ํ์{0:yy-M-d tt H:m:s (ddd)}", dt);
}
}
}
์ถ๋ ฅ
12์๊ฐ ํ์2022-02-07 ์คํ 05:48:01 (์)
24์๊ฐ ํ์2022-02-07 ์คํ 17:48:01 (์)
24์๊ฐ ํ์22-2-7 ์คํ 17:48:1 (์)
using System.Globalization;์ ์ฌ์ฉํ๋ฉด ์์ด ์ฌ์ฉ์์ ํ๊ธ ์ฌ์ฉ์ ๊ฐ๊ฐ์๊ฒ ์๋ง์ ํค์๋๋ฅผ ๋ณด์ฌ์ค ์ ์๋ค.
(ํ ์์ผ vs Sat(ํน์ Saturday))
(์ค์ ์คํ vs AM PM)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
DateTime dt = new DateTime(2022, 02, 07, 17, 48, 01);
//12์๊ฐ ํ์
WriteLine("12์๊ฐ ํ์{0:yyyy-MM-dd tt hh:mm:ss (ddd)}",dt);
//24์๊ฐ ํ์
WriteLine("24์๊ฐ ํ์{0:yyyy-MM-dd tt HH:mm:ss (ddd)}", dt);
WriteLine("24์๊ฐ ํ์{0:yy-M-d tt H:m:s (ddd)}", dt);
CultureInfo ciko = new System.Globalization.CultureInfo("ko-KR");
WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)",ciko));
CultureInfo ciEn = new System.Globalization.CultureInfo("en-US");
WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciEn));
}
}
}
์ถ๋ ฅ
12์๊ฐ ํ์2022-02-07 ์คํ 05:48:01 (์)
24์๊ฐ ํ์2022-02-07 ์คํ 17:48:01 (์)
24์๊ฐ ํ์22-2-7 ์คํ 17:48:1 (์)
2022-02-07 ์คํ 05:48:01 (์)
2022-02-07 PM 05:48:01 (Mon)
์ต์ข
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
DateTime dt = new DateTime(2022, 02, 07, 18, 09, 01);
WriteLine("12์๊ฐ ํ์: {0:yyyy-MM-dd tt hh:mm:ss(ddd)}",dt);
WriteLine("24์๊ฐ ํ์: {0:yyyy-MM-dd HH:mm:ss(dddd)}", dt);
CultureInfo ciko = new CultureInfo("ko-KR");
WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciko));
WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss (dddd)", ciko));
WriteLine(dt.ToString(ciko));
CultureInfo ciEn = new CultureInfo("en-US");
WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciEn));
WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss (dddd)", ciEn));
WriteLine(dt.ToString(ciEn));
}
}
}
์ถ๋ ฅ
12์๊ฐ ํ์: 2022-02-07 ์คํ 06:09:01(์)
24์๊ฐ ํ์: 2022-02-07 18:09:01(์์์ผ)
2022-02-07 ์คํ 06:09:01 (์)
2022-02-07 18:09:01 (์์์ผ)
2022-02-07 ์คํ 6:09:01
2022-02-07 PM 06:09:01 (Mon)
2022-02-07 18:09:01 (Monday)
2/7/2022 6:09:01 PM
-3- ๋ฌธ์์ด ๋ณด๊ฐ
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int n = 123;
WriteLine($"{(n>100?"ํผ":"์์")}"); //ํผ
}
}
}
์ต์ข ์์ค์ฝ๋
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
string name = "๋ฐ๋จ๋จ";
int age = 23;
WriteLine($"{name,-10},{age:D3}");
name = "๋ฐ์ฐ์ฝ";
age = 33;
WriteLine($"{name},{age,-10:D3}");
name = "๋ฐ์ด์";
age = 28;
WriteLine($"{name},{(age>19 ? "์ฑ์ธ" :"๋ฏธ์ฑ๋
์")}");
}
}
}
๋ฐ๋จ๋จ ,023
๋ฐ์ฐ์ฝ,033
๋ฐ์ด์,์ฑ์ธ
์ฐ์ต ๋ฌธ์ ํ์ด
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
WriteLine("์ฌ๊ฐํ์ ๋๋น๋ฅผ ์
๋ ฅํ์ธ์.");
string width = ReadLine();
int wid = int.Parse(width);
WriteLine("์ฌ๊ฐํ์ ๋์ด๋ฅผ ์
๋ ฅํ์ธ์.");
string height = ReadLine();
int hei = int.Parse(height);
WriteLine($"๋๋น๋ {width}, ๋์ด๋ {height}์ด๋ฏ๋ก, ์ฌ๊ฐํ์ ๋๋น๋{wid* hei} ์
๋๋ค.");
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
์ฌ๊ฐํ์ ๋๋น๋ฅผ ์
๋ ฅํ์ธ์.
30
์ฌ๊ฐํ์ ๋์ด๋ฅผ ์
๋ ฅํ์ธ์.
40
๋๋น๋ 30, ๋์ด๋ 40์ด๋ฏ๋ก, ์ฌ๊ฐํ์ ๋๋น๋1200 ์
๋๋ค.
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
12. ๋นํธ ์ฐ์ฐ์ (0) | 2022.02.08 |
---|---|
11. null ์กฐ๊ฑด๋ถ ์ฐ์ฐ์ (0) | 2022.02.08 |
9. ๊ณต์ฉ ํ์ ์์คํ (0) | 2022.02.07 |
8. Var, Object ํ์๊ณผ์ ์ฐจ์ด? (0) | 2022.02.07 |
7. Nullable, HasValue, Value (0) | 2022.02.07 |