C#
C# String 관련 함수
seyeol
2023. 3. 4. 15:01
반응형
string Word = "Good Morning";
Console.WriteLine(Word);
//Indexof는 문자열이 속한 위치를 찾아주는것
WriteLine("Indexof 'Good' : {0}", Word.IndexOf("Good"));
WriteLine("Indexof 'd' : {0}", Word.IndexOf("d"));
//LastIndexOf는 뒤에서 부터 문자열 위치 찾아줌
WriteLine("LastIndexOf 'Good' : {0} ", Word.LastIndexOf("Good"));
WriteLine("LastIndexOf 'g' : {0} ", Word.LastIndexOf("g"));
//StartWith는 True False 반환시켜줌 그 문자열로 시작한다면
WriteLine("StartWith 'Good' : {0}", Word.StartsWith("Good"));
//EndWith는 True False 반환함 그 문자열로 끝난다면
WriteLine("EndWith 'Morning' : {0}", Word.EndsWith("Morning"));
//Contains는 지정한 문자열이 포함 되어있다면 True ,False 반환함
WriteLine("Contains 'Morning' : {0}", Word.Contains("Morning"));
//Replace 는 지정한 단어를 다른 단어로 바꿔줌
WriteLine("Replce 'Good' : {0}", Word.Replace("Good","Every"));
Console.WriteLine("ToLower() : {0}", "ABC".ToLower());//대문자를 소문자로 바꿔줌
Console.WriteLine("ToUpper() : {0}", "abc".ToUpper());//소문자를 대문자로 바꿔줌
Console.WriteLine("Insert() : {0}", "ABC".Insert(0,"바껴라")); //Insert(index , 들어갈 string) 이런 형식으로 지정한 Index에 string 넣어줌
Console.WriteLine("Remove() : {0}", "Remove".Remove(0,3));//Remove(index , count) 까지의 문자 삭제해줌
Console.WriteLine("Trim() : {0} ", " No Space ".Trim()); //앞뒤 공백을 업에줌 " hello " -> "hello"
Console.WriteLine("TrimStart() : {0}", " No Space ".TrimStart()); //앞의 공백없에줌 " hello" -> "hello"
Console.WriteLine("TrimEnd() : {0}", " No Space ".TrimEnd()); //뒤의 공백 없에줌 "hello " -> "hello"
string Test = "Good Morning";
WriteLine(Test.Substring(0, 5));//(시작 위치 , 끝나는 위치) 반환해줌
WriteLine(Test.Substring(5)); //(시작위치) 시작위치 ~~~ 로 반환해줌
string[] arr = Test.Split(new string[] {" "}, StringSplitOptions.None); // Split(끊어줄 문자) 문자열을 지정한 문자를 기준으로 끊어서 새로운 string을 만들어줌
WriteLine("arr Count : {0}", arr.Length);
foreach(string element in arr) //foreach (변수타입 변수명 배열이나 컬렉션의 데이터)의 변수를 foreach의 지역변수에 저장
{
WriteLine("{0}", element); 지역변수element에 저장한 data를 출력
}
string fmt = "{0,-20}{1,-15}{2,20}"; // string format {번호, - or + 위치} -이면 위치만큼 뒤에 글자 칸수를 밀어놓고 왼쪽에 배치 + 이면 앞의 문자열에서 위치 만큼 떨어져서 배치
Console.WriteLine(fmt, "이름:seyeol", "나이: 1세", "외모:잘생김");
//D : 10진수
WriteLine("10진수 : {0:D}", 123);
WriteLine("10진수 : {0:D5}", 123);
//X : 16진수
WriteLine("16진수 {0:X}",123);
WriteLine("16진수 {0:X8}", 123);
//N : 숫자
WriteLine("숫자 {0:N}",12345678);
WriteLine("숫자 {0:N0}", 123456789.123); //소수점 0이하 삭제함
//F : 고정소수점
WriteLine("고정소수점 : {0:F}", 123.45);
WriteLine("고정소수점 : {0:F1}", 123.45);
//E : 공학용
WriteLine("공학 : {0:E}", 123.456789);