연습문제/C# 연습문제
8. 문자열 치환, 문자열 변경(특정문자)
규남
2023. 8. 6. 22:18
반응형
<소스 코드>
namespace Test
{
internal class Program
{
static void Main(string[] args)
{
string str = "abcdef";
string tmp = "g";
// 주어진 문자열중 특정문자만 바꾸기
// a -> g로 변경
string result = str.Replace("a", tmp);
Console.WriteLine(result);
// 특정 문자만 대소문자로 변경하기(응용버전)
// 기준은 특정문자가 대문자로 변경할것인지
// 소문자로 변경할것인지 먼저 정하기
// ex) a -> A로 변경, 나머지는 소문자로
// 1. 일단 모두 소문자로 변경해둔다.
str = "abcdEFG";
str = str.ToLower();
// 2. 모두 소문자이기 때문에 a만 A로 바꾸면된다.
result = str.Replace("a", "A");
Console.WriteLine(result);
}
}
}
<결과 확인>
728x90
반응형