반응형

전체 글 159

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

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

반응형