반응형

연습문제 23

10. 공백(다중) 처리하기

namespace Test { internal class Program { static void Main(string[] args) { // 기본적인 공백기준 확인방법 // Split은 해당부분을 기준으로 나눈다 string str = "hello world!"; string[] strings = str.Split(' '); for(int i = 0; i < strings.Length; i++) { Console.WriteLine($"{i} = {strings[i]}"); } Console.WriteLine(); Console.WriteLine(); // 그런데 위에 방식대로 단순 Split를 쓸경우 // 아래와 같이 다중으로 공백이 들어가면 문제발생 str = " hello world! "; strin..

9. 제곱근과 제곱수

namespace Test { internal class Program { static void Main(string[] args) { // 제곱근 구하기 // Math.Sqrt => (double)반환형 int n = 9; double result = Math.Sqrt(n); Console.WriteLine(result); Console.WriteLine("======"); // 제곱수인지 판별하는법 (제곱근 % 1) // Math.Sqrt로 나온 결과물에 1로 나눈 나머지가 // 0이면 n은 제곱수이다. n = 10; result = Math.Sqrt(n) % 1; Console.WriteLine(result); n = 9; result = Math.Sqrt(n) % 1; Console.WriteLi..

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] - ..

반응형