반응형

전체 글 158

8. 문자열 치환, 문자열 변경(특정문자)

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. 모두 소문자이기..

7. 대소문자 구별없이 비교하기(Contains)

namespace Test { internal class Program { static void Main(string[] args) { string str1 = "abcd"; string str2 = "aBcD"; // 대소문자 구별없이 확인방법 // Contains 사용시 다르다고 나온다!!! if(str1.Contains(str2)) Console.WriteLine("두 문자열이 같다"); else Console.WriteLine("두 문자열이 다르다"); Console.WriteLine("=================================="); // 해결방법은? // StringComparison.OrdinalIgnoreCase 인자값을 추가해주면 if (str1.Contains(str2..

6. 대소문자 변환

namespace Test { internal class Program { static void Main(string[] args) { // 소문자 -> 대문자 string str1 = "abcdefg"; string result1 = str1.ToUpper(); Console.WriteLine(result1); // 아스키코드를 이용한 방법 result1 = string.Empty; for(int i = 0; i < str1.Length; i++) { // 소문자 a = 97, 대문자 A = 65 // 즉 소문자에서 대문자로 바꾸려면?? // 97에서 32만큼 빼주면 65가 되므로 // 문자로 A가 만들어진다. 원리를 이해하는데 // 도움이 되는 방법 result1 += (char)(str1[i] - ..

4. 문자열에서 숫자 찾기

namespace Test { internal class Program { static void Main(string[] args) { string my_string = "12345abcd"; string result = string.Empty; // 방법 1 for(int i = 0; i < my_string.Length; i++) { // IsDigit(char) : 문자가 10진수인지 아닌지 판단 // IsDigit(string, int32) // 지정된 문자열(string)의 몇번째 위치(int32)에 // 해당하는 문자가 10진수인지 판단 // 맞으면 true, 아니면 false if (Char.IsDigit(my_string[i]) == true) result += my_string[i]; }..

3. 숫자의 각 자릿수 더하기

namespace Test { internal class Program { static void Main(string[] args) { // 각 자리수의 합 계산하기 // 1. 단순 계산 알고리즘으로 나타내기 int n = 1234; int result = 0; while (n > 0) { result += n % 10; n = n / 10; } Console.WriteLine(result); // 2. 배열을 이용한 계산 // 숫자를 문자열로 바꾼뒤에 각각의 문자는 해당 아스키 코드를 // 가지고 있으므로 (문자 - 문자)를 통해 실제 숫자로 바꿔준다. // ('1' - '0') 은 아스키코드로 (49 - 48)이므로 1이 나온다. // 이것을 (int)형으로 변환하면 숫자 1로 바뀐다. // 만약 단..

반응형