문자열 첫번째 문자를 제거하거나, 앞에서 n개의 문자를 제거하는 방법을 소개합니다.
1. string.Substring()를 이용한 방법
string.Substring(startIndex)는 startIndex 부터 문자열 마지막까지 잘라서 문자열로 리턴합니다.
따라서, Substring(1)은 첫번째 문자 1개가 제거된 문자열이되고, Substring(2)는 앞의 문자 2개가 제거된 문자열이 됩니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
string str = "Hello, World, C#";
string result = str.Substring(1);
Console.WriteLine(result);
result = str.Substring(2);
Console.WriteLine(result);
}
}
}Output:
ello, World, C#
llo, World, C#2. string.Remove()를 이용한 방법
string.Remove(startIndex, count)는 startIndex 부터 count 개수의 문자가 제거된 문자열을 리턴합니다.
따라서, Remove(Length, 1)은 첫번째 문자 1개가 제거된 문자열이되고, Remove(Length, 2)는 앞의 문자 2개가 제거된 문자열이 됩니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
string str = "Hello, World, C#";
string result = str.Remove(str.Length - 1);
Console.WriteLine(result);
result = str.Remove(str.Length - 2);
Console.WriteLine(result);
result = str.Remove(str.Length - 4);
Console.WriteLine(result);
}
}
}Output:
ello, World, C#
llo, World, C#Loading script...
Related Posts
- C# 바이트(Byte) 배열을 문자열로 변환
- C# String.format() 함수 알아보기
- C# - foreach (Array, List, Dictionary)
- C# 프로퍼티(Property) Get/Set 함수
- C# - Dictionary에서 key, value 가져오기
- C# - Dictionary 순회, foreach, for 루프
- C# - Dictionary에서 key, value 삭제
- C# - Dictionary.add()로 데이터 추가
- C# - Dictionary 선언 및 초기화
- C# - 문자열을 Double, Float으로 변환
- C# - 문자열을 리스트로 변환
- C# - 두 날짜/시간 비교, DateTime.Compare()
- C# - 날짜 문자열을 DateTime으로 변환
- C# - Sleep, 몇 초간 지연시키기
- C# - 날짜 계산, DateTime 시간 더하기 빼기
- C# - Timestamp(millisecond)를 DateTime 객체로 변환
- C# - 현재 시간 가져오기, DateTime
- C# - 문자열 리스트를 문자열로 변환
- C# - 리스트 복사 (얕은 복사, 깊은 복사)
- C# - 2차원 리스트 선언 및 초기화
- C# - 리스트 선언 및 초기화
- C# - 리스트 길이, 크기 가져오기
- C# - 리스트 합계, 평균 계산
- C# - 리스트 요소 제거 (RemoveAt, RemoveAt, RemoveAll)
- C# - 리스트에서 빈 문자열, null 제거
- C# - 리스트 두개로 분리, n개로 나누기
- C# - 문자열 뒤집기, Reverse()
- C# - 2차원 배열 복사, Clone
- C# - 문자열 배열을 문자열로 변환
- C# - 2차원 배열 선언, 초기화 방법
- C# - 배열 길이, 2차원 배열 길이
- C# - 두개의 배열을 하나로 합치기
- C# - 배열 중복 요소 제거
- C# - 배열에서 특정 요소 제거
- C# - Int 배열 정렬 (오름차순, 내림차순)