StringCollection
StringCollection 은 문자열의 컬렉션을 나타내는 .NET Framework class Library 이다.
cf) MSDN : StringCollection
여러 문자열을 관리해야될 필요가 있을때 배열인 string[] 을 쓰거나 컬렉션으로 List<string> 을 사용하였는데 StringCollection 클래스가 있다는 것을 알게되었다. 사용방법을 알아두고 필요할 때 사용하자!
Creating StringCollection
StringCollection 객체를 생성하는 방법이다.
StringCollection authorNames = new StringCollection();
Adding Strings
StringCollection 은 string collection 에 string 을 추가하는 방법으로 다음 3가지를 제공해 준다.
● Add
● AddRange
● Insert
Add
Add 메소드는 StringCollection 의 string collection 마지막에 string 을 추가한다.
authorNames.Add("Mahesh Chand");
authorNames.Add("Mike Gold");
authorNames.Add("Praveen Kumar");
authorNames.Add("Raj Beniwal");
AddRange
AddRagne 메소드는 StringCollection 의 string collection 에 string 배열을 추가할 때 사용한다.
string[] names = new string[]{"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"};
authorNames.AddRange(names);
Insert
Insert 메소드는 StringCollection 의 string collection 에 지정한 위치에 string 을 추가한다.
지정한 위치 인덱스가 0 보다 작거나 StringCollection 의 아이템 개수보다 크다면 ArgumentOutOfRangeException 예외가 발생한다.
authorNames.Insert(5, "New Author");
Accessing Strings
StringCollection 저장한 string 데이터들을 읽는 방법을 알아보자. 간단하게 foreach 구문을 이용하여 읽으면 된다.
StringCollection authorNames = new StringCollection();string[] names = new string[]{"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"};
authorNames.AddRange(names);foreach (string name in authorNames){ Console.WriteLine(name);}
Removing Strings
StringCollection 은 string collection 을 삭제하기 위한 3가지 메소드를 제공해준다.
● Clear
● Remove
● RemoveAt
Clear
Clear 메소드는 StringCollection 의 모든 string 아이템을 삭제한다.
authorNames.Clear();
Remove
Remove 메소드는 지정한 문자열이 StringCollection 의 string 아이템 중 첫 번째로 발견한 아이템을 삭제한다.
authorNames.Remove("Mike Gold");
RemoveAt
RemoveAt 메소드는 StringCollection 에서 지정한 위치의 string 아이템을 삭제한다. 인덱스를 사용하는 메소들과 마찬가지로 인덱스가 0보다 작거나 유효하지 않은 인덱스일 경우 ArgumentOutOfRangeException 예외가 발생한다.
authorNames.RemoveAt(5);
Find String
StringCollection 에서 원하는 string 을 찾는 방법을 알아보자.
IndexOf
indexOf 메소드는 찾을 문자열을 메소드로 전달한다. StringCollection 에서 처음 발견한 아이템의 인덱스(zero-base)를 리턴한다.
int authorLocation = authorNames.IndexOf("Mike Gold");Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString());
Contains
Contains 메소드는 찾을 문자열을 메소드로 전달한다. StringCollection 에 해당 문자열이 존재하면 true, 존재하지 않으면 false 를 리턴한다.
if (authorNames.Contains("Mike Gold")){ Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"));}
Copy Strings
StringCollection 의 string collection 을 복사하는 방법을 알아보자.
CopyTo
CopyTo 메소드를 사용하면 StringCollection 의 string collection 을 array 로 복사한다. CopyTo 메소드는 두개의 매개변수를 받는다. 첫 번째 매개변수는 StringCollection 의 string collection 을 복사 받을 array 이름이다. 두 번째 매개변수는 array 에서 복사를 시작할 인덱스 이다.
string[] newAuthorList = new string[authorNames.Count];
authorNames.CopyTo(newAuthorList, 0);
foreach (string name in newAuthorList)
{
Console.WriteLine(name);
}
Count Strings
Count 프로퍼티를 이용하면 StringCollection 의 아이템의 개수를 알 수있다.
Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString());
Getting Items
StringCollection 은 Collection 이기 때문에 인덱서를 사용할 수 있다. 인덱서를 사용하여 아이템을 얻을 수 있다.
int authorLocation = authorNames.IndexOf("Mike Gold");
string authorName = authorNames[authorLocation];
Complete Code
위에서 설명한 메소드들을 사용한 예제이다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace StringCollectionSample
{
class Program
{
static void Main(string[] args)
{
// Create a StringCollection object
StringCollection authorNames = new StringCollection();
// Add string using Add method
authorNames.Add("Mahesh Chand");
authorNames.Add("Mike Gold");
authorNames.Add("Praveen Kumar");
authorNames.Add("Raj Beniwal");
// Add an array of string using AddRange
string[] names = new string[]{"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"};
authorNames.AddRange(names);
// Insert an string at a specified index
authorNames.Insert(5, "New Author");
// authorNames.Clear();
// authorNames.Remove("Mike Gold");
// authorNames.RemoveAt(5);
if (authorNames.Contains("Mike Gold"))
{
Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"));
}
int authorLocation = authorNames.IndexOf("Mike Gold");
string authorName = authorNames[authorLocation];
Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString());
Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString());
foreach (string name in authorNames)
{
Console.WriteLine(name);
}
// Copy Collection to new Array
string[] newAuthorList = new string[authorNames.Count];
authorNames.CopyTo(newAuthorList, 0);
foreach (string name in newAuthorList)
{
Console.WriteLine(name);
}
//int[] intArray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
//foreach (int i in intArray)
//{
// System.Console.WriteLine(i);
//}
Console.ReadLine();
}
}
}

cf)
원문 포스트를 읽다보니 리플로 StringCollection 은 전혀 새롭게 추가된 것도 아니며 List<string> 을 사용하는게 더 쉽다고 남겨져 있다. 원문 작성자는 개발자가 List<>를 잘 모를 경우 StringCollection 을 사용하는게 시간 절약이라고 말하고 있다. 음.. List 컬렉션에 익숙해 지는게 더 이익인것 같다.
댓글 없음:
댓글 쓰기