using System; public class 문자열관련클래스 { public static void Main(string[] args) { #region 문자열 클래스 string s = ""; s = String.Empty; // 빈값 → 필드 s = " Abc Def Fed Cba "; Console.WriteLine(s.Length); // [1]속성 Console.WriteLine(s.ToUpper()); // [2] Console.WriteLine(s.ToLower()); Console.WriteLine("[{0}]", s.TrimStart()); // [3] Console.WriteLine("[{0}]", s.TrimEnd()); // [4] Console.WriteLine("[{0}]", s.Trim()); // [5] Console.WriteLine(s.Replace("Def", "디이에프").Trim().ToLower()); // [6] Console.WriteLine("위치값(앞에서부터 검색) : " + s.IndexOf('e')); // [7] 문자열의 위치값(n-1) Console.WriteLine("위치값(뒤에서부터 검색) : " + s.LastIndexOf('e'));// [8]문자열의 위치값을 뒤에서부터 검색 Console.WriteLine(s.Substring(5, 3)); // [9]5번째 부터 3글자 읽어오기 Console.WriteLine(s.Substring(10)); // [10]10번째이후 문자 모두 읽어오기 Console.WriteLine(s.Insert(0, "안녕").Remove(3, 4)); // [11] //[12] 구분자로 분리 string ss = "축구,배구,농구"; string[] arr = ss.Split(','); for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); } //[13] ~로 시작 또는 끝나는지 알고자 한다면 string url = "http://www.dotnetkorea.com"; Console.WriteLine(url.StartsWith("http://")); Console.WriteLine(url.EndsWith(".com")); // [14] 복잡한 문자열 연결 string js = String.Format("{0}\n{1}\n{2}",""); Console.WriteLine(js); //[QUIZ] string dir = "C:\\Home\\abc\\abc.com\\Default.aspx"; string file = ""; string name = ""; string ext = ""; file = dir.Substring(dir.LastIndexOf("\\") + 1); name=file.Substring(0,file.LastIndexOf(".")); ext=file.Substring(file.LastIndexOf('.')+1); //name = dir.Substring(dir.LastIndexOf('\\') + 1,dir.LastIndexOf('.')-dir.LastIndexOf('\\')-1); Console.WriteLine("파일명 : {0}",file); Console.WriteLine("파일명 : {0}", name); //Default Console.WriteLine("확장자 : {0}", ext); //aspx //과제 Console.WriteLine("파일명 : {0}", MyFile(dir)); Console.WriteLine("파일명 : {0}", MyName(dir)); //Default Console.WriteLine("확장자 : {0}", MyExt(dir)); //aspx #endregion #region 수학관련클래스 Console.WriteLine("자연로그 = {0}",Math.E); Console.WriteLine("원주율 + {0}",Math.PI); Console.WriteLine("-10의 절대값 : {0}",Math.Abs(-10)); Console.WriteLine("-10의 절대값 : {0}", MyAbs(-10)); int a = 3, b = 5; Console.WriteLine("3과 5중 큰수 : {0}",Math.Max(a,b)); Console.WriteLine("3과 5중 큰수 : {0}", MyMax(a, b)); Console.WriteLine("3과 5중 작은수 : {0}", Math.Min(a, b)); Console.WriteLine("3과 5중 작은수 : {0}", MyMin(a, b)); Console.WriteLine("2의 10승 : {0}", Math.Pow(2,10)); Console.WriteLine("2의 10승 : {0}", MyPow(2, 10)); Console.WriteLine("3.15를 소숫점 둘째자리에서 반올림 : {0}", Math.Round(3.14159,2)); Console.WriteLine("3.15를 소숫점 둘째자리에서 반올림 : {0}", MyRound(3.14159, 3)); #endregion } private static double MyRound(double a, int b) { return (int)((a + 5 * MyPow(0.1, b + 1)) * MyPow(10, b)) / MyPow(10, b); } private static double MyPow(double a, int b) { double sum = 1; for (int l = 1; l <= b; l++) { sum = sum*a; } return sum; } private static int MyMin(int a, int b) { return (a < b) ? a : b; } private static int MyMax(int a, int b) { return (a < b) ? b : a; } private static int MyAbs(int n) { return (n < 0) ? -n : n; } public static string MyFile(string s) { return s.Substring(s.LastIndexOf("\\") + 1); } public static string MyName(string s) { return s.Substring(s.LastIndexOf('\\') + 1, s.LastIndexOf('.') - s.LastIndexOf('\\') - 1); } public static string MyExt(string s) { return s.Substring(s.LastIndexOf('.')+1); } }