S_pot

210520 C#_IndexOf, LastIndexOf 본문

C#

210520 C#_IndexOf, LastIndexOf

S_pot 2021. 5. 20. 10:41
 static void Main1(string[] args)
        {
            char[] byteArray = new char[] { '가', '나', '다' };
            string aString1 = "테스트";
            string aString2 = new string(byteArray);

            Console.WriteLine(aString1);
            Console.WriteLine(aString2);
            int iNum = "테스트".IndexOf('테');       // "테스트"를 앞서 String으로 선언했기 때문에 "테스트"를 String과 같게 인식한다.
            Console.WriteLine(iNum);                // '테' = 0, '스' = 1, '트' = 2, 없는 문자는 -1로 출력된다.
            
            iNum = "지옥으로 지 키티".IndexOf('지'); // IndexOf: 앞에서부터 검색 해서 첫번째 '지'를 int형으로 출력
            Console.WriteLine(iNum);                 // '지' = 0   
            iNum = "지옥으로 지 키티".LastIndexOf('지');  // Last IndexOf: 뒤에서부터 검색해서 두번째 '지'를 int형으로 출력
            Console.WriteLine(iNum);                      // '지' = 5  
            
            bool bResult = "지옥으로 지 키티".Contains("지옥");  // Contains: 문자열안에 "지옥"이 있으면 bool형으로 True를 반환
            Console.WriteLine(bResult);
        }