[파일명:  TestStringFindApp.cs]------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;

namespace MyTestApplication1 {

    class TestStringFindApp {

        public static void Main(String[] args) {
            String[] words = new String[] { "하나", "둘", "셋", "넷", "다섯", "여섯" };
            int where;

            Console.Write("array: ");
            PrintArray(words);
            where = Find(words, "셋");
            if (where >= 0) {
                Console.Write("발견!  ");
                Console.WriteLine("Next word of 셋 in array: " + words[where+1]);
            }

            Console.WriteLine("Sorting...");
            Array.Sort(words);

            Console.Write("array: ");
            PrintArray(words);
            where = Find(words, "셋");
            if (where >= 0) {
                Console.Write("발견!  ");
                Console.WriteLine("Next word of 셋 in array: " + words[where+1]);
            }
        }

        public static int Find(String[] arr, String s) {
            for (int i = 0; i < arr.Length; i++) {
                if (arr[i].IndexOf(s) >= 0)
                    return i;
            }
            return -1;
        }

        public static void PrintArray(Object[] arr) {
            Console.Write("[");
            for (int i = 0; i < arr.Length - 1; i++) {
                Console.Write("{0}, ", arr[i]);
            }
            if (arr.Length > 0)
                Console.Write("{0}", arr[arr.Length - 1]);
            Console.WriteLine("]");
        }
    }
}
------------------------------------------------


컴파일> csc testStringFindApp.cs

실행> TestStringFindApp
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,