C# 문자열

2023. 3. 12. 01:41C#

반응형
class MainApp
    {
        
        static void Main(string[] args)
        {
            
            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) 
            {
                WriteLine("{0}", element);
            }


        }
            
        

       
        


    }

'C#' 카테고리의 다른 글

C# String 보간  (0) 2023.03.04
C# DateTime  (0) 2023.03.04
C# String 관련 함수  (2) 2023.03.04