// 중요 파트
using System;
public class 스트링클래스
{
public static void Main(string[] args)
{
string s = String.Empty; // 빈 문자열 저장
s = " "; // 일반적으로 많이 쓰는 표현. NULL값 아님
s = " Abc Def Fed Cba "; // 테스트용 문자열 저장. 앞 뒤로 한칸씩
Console.WriteLine("{0}", s); // 전체 출력
// 문자열 중 6번째 인덱스의 문자 하나 출력.
// 인덱스는 0부터 셈 : e
// [ 7 - 1 ] 식의 연산식도 가능
Console.WriteLine(s[7 - 1]);
// 문자열 길이. 인덱스와 달리 길이는 1부터 셈 : 17
Console.WriteLine(s.Length);
Console.WriteLine(s.ToUpper()); // 대문자
Console.WriteLine(s.ToLower()); // 소문자
Console.WriteLine(s.TrimStart()); // 앞에 있는 공백 제거
Console.WriteLine(s.TrimEnd()); // 뒤에 있는 공백 제거
Console.WriteLine(s.Trim()); // 양쪽 공백 제거
Console.WriteLine(s.Replace("Def", "deF").Replace('F', 'f'));
( String oldValue -> String newValue )
// 문자 z의 위치(인덱스)값? 앞에서 부터 찾음.
// 조건 만족하는 문자 데이터 없을 때는 -1 반환
Console.WriteLine(s.IndexOf('z'));
// 문자 e의 위치(인덱스)값? 앞에서 부터 6번째. 인덱스는 0부터 시작 : 6
Console.WriteLine(s.IndexOf("e"));
// 문자 e의 위치(인덱스)값을 뒤에서 부터
검색해서 해당 문자의 인덱스 값 변환
Console.WriteLine(s.LastIndexOf("e"));
// 5번째 인덱스부터 3자 출력
Console.WriteLine(s.Substring(5, 3));
// 5번째 인덱스부터 모두 출력
Console.WriteLine(s.Substring(5));
// 5번째 인덱스부터 3자 삭제
Console.WriteLine(s.Remove(5, 3));
// 구분자 (공백, 콤마)를 기준으로 사용하여 분리 저장
string[] arr = s.Trim().Split(' ');
// arr[0] = "Abc";
// arr[1] = "Def";
// arr[2] = "Fed";
// arr[3] = "Cba";
foreach (string one in arr)
{
Console.WriteLine(one);
}
string url = "https://sify.tistory.com/";
if (!url.StartsWith("http://"))
{
Console.WriteLine("{0}\n URL은 https: //로 시작해야 합니다.", url);
}
if (url.Substring(url.Length-1) == "/")
{
string temp = url.Remove(url.Length - 1, 1);
if (temp.EndsWith(".com"))
{
Console.WriteLine("{0}\n .com으로 끝나는군요.", temp);
}
}
}
}
- String Class
http://msdn.microsoft.com/ko-kr/library/system.string_members(VS.95).aspx
'.Net Project > .Net C#' 카테고리의 다른 글
26장 파일명추출 (0) | 2009.08.10 |
---|---|
25장 String Format (0) | 2009.08.10 |
23장 알고리즘(간단수열) (0) | 2009.08.10 |
22장 알고리즘(간단수열) (0) | 2009.08.10 |
21장 수열(간단수열) (0) | 2009.08.10 |