문자열 처리 기능 및 메서드
- C#에서는 문자열을 처리하는 다양한 기능과 메서드를 제공한다.
1. 생성
string str1 = "Hello, World!";
string str2 = new string('H', 5);
Console.WriteLine(str1);
Console.WriteLine(str2);
2. 연결
string str1 = "Hello";
string str2 = "World";
string str3 = str1 + " " + str2;
Console.WriteLine(str3);
3. 분할
string str = "Hello, World!";
string[] words = str.Split(',');
Console.WriteLine(words[0]);
Console.WriteLine(words[1]);
4. 검색
string str = "Hello, World!";
int index = str.IndexOf("World");
Console.WriteLine(index);
5. 대체
string str = "Hello, World!";
string newStr = str.Replace("World", "Universe");
Console.WriteLine(str);
Console.WriteLine(newStr);
6. 변환
- Convert
string str = "123";
int num = Convert.ToInt32(str);
- Parse
string str = "123";
int num = int.Parse(str);
- TryParse
string str = "123";
int num;
bool isSuccess = int.TryParse(str, out num);
int num = 123;
string str = num.ToString();
- 형변환 리스트

7. 비교
string str1 = "Hello";
string str2 = "World";
bool isEqual = str1 == str2;
Console.WriteLine(isEqual);
string str1 = "Apple";
string str2 = "Banana";
int compare = string.Compare(str1, str2);
Console.WriteLine(compare);
8. 포맷팅
string name = "John";
int age = 30;
string message = string.Format("My name is {0} and I'm {1} years old.", name, age);
Console.WriteLine(message);
string name = "John";
int age = 30;
string message = $"My name is {name} and I'm {age} years old.";
Console.WriteLine(message);