스트링 리스트에서 스트링 찾기(find) with C#
[파일명: TestStringFindInList.cs]------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
namespace MyTestApplication1 {
class TestStringFindInList {
public static void Main(String[] args) {
List<string> words = new List<string>(new String[] { "하나", "둘", "셋", "넷", "다섯", "여섯" });
int where;
Console.Write("list: ");
PrintArray(words);
where = Find(words, "셋");
if (where > 0) {
Console.Write("발견! ");
Console.WriteLine("Next word of 셋 in list: " + words[where+1]);
}
Console.WriteLine("Sorting...");
words.Sort();
Console.Write("list: ");
PrintArray(words);
where = Find(words, "셋");
if (where > 0) {
Console.Write("발견! ");
Console.WriteLine("Next word of 셋 in list: " + words[where+1]);
}
}
public static int Find(List<string> arr, String s) {
for (int i = 0; i < arr.Count; i++) {
if (arr[i].IndexOf(s) >= 0)
return i;
}
return -1;
}
public static void PrintArray(List<string> arr) {
Console.Write("[");
for (int i = 0; i < arr.Count - 1; i++) {
Console.Write("{0}, ", arr[i]);
}
if (arr.Count > 0)
Console.Write("{0}", arr[arr.Count - 1]);
Console.WriteLine("]");
}
}
}
------------------------------------------------
컴파일> csc testStringFindInList.cs
실행> TestStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견! Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견! Next word of 셋 in list: 여섯