C# 에서 left, right, mid 함수 사용방법을 아래와 같이 정리하였다.
using System;
class program {
// Left string
public static string Left(string Text, int TextLength) {
string ConvertText;
if (Text.Length < TextLength) {
TextLength = Text.Length;
}
ConvertText = Text.Substring(0, TextLength);
return ConvertText;
}
// Right string
public static string Right(string Text, int TextLength) {
string ConvertText;
if (Text.Length < TextLength) {
TextLength = Text.Length;
}
ConvertText = Text.Substring(Text.Length - TextLength, TextLength);
return ConvertText;
}
// Mid string
public static string Mid(string Text, int Startint, int Endint) {
string ConvertText;
if (Startint < Text.Length || Endint < Text.Length) {
ConvertText = Text.Substring(Startint, Endint);
return ConvertText;
}
else
return Text;
}
private static void Main( ) {
string num1 = " 1 2 3 4";
string num2 = "012345678901234567890123456789012345678901";
string test = "Hello, C# is a great programming language.";
string l5 = Left(test, 5);
string r9 = Right(test, 9);
string m155 = Mid(test, 15, 5);
Console.WriteLine("\n" + num1);
Console.WriteLine(num2);
Console.WriteLine(test + "\n");
Console.WriteLine("Left 5 chars: \"" + l5 + "\"");
Console.WriteLine("Right 9 chars: \"" + r9 + "\"");
Console.WriteLine("Mid 5 chars from the position 15: \"" + m155 + "\"");
}
}
참고로, 뜬금 없지만 윈도우즈 명령에 대한 단축키 사이트를 소개 한다.
실행 결과는 아래와 같다.

'C# 언어' 카테고리의 다른 글
| 클립보드 내용을 파일로 만들기 (0) | 2021.06.27 |
|---|---|
| C#으로 달력 만들기 (0) | 2021.05.15 |
| C# 에서 List 추가 및 정렬하기 (0) | 2021.01.14 |
| C# 으로 시계 만들기 (2) (0) | 2021.01.11 |
| C# Timer 를 이용해서 시계 만들기 (0) | 2021.01.10 |
댓글