본문 바로가기
C++ 200제/코딩 IT 정보

MFC C# 문자열 자르기 비교 - CString String Mid, Left, Right

by vicddory 2017. 3. 7.

MFC(CString class)에서 Left, Mid, Right를 사용하여 문자열 자르기하는 방법은 아래와 같습니다.


MFC (CString) Samples: 

1
2
3
4
5
6
7
8
9
10
CString somestring = L"ABCDEFG";
 
somestring.Left(3== L"ABC"
 
somestring.Mid(2,3== L"CDE"
somestring.Mid (2== L"CDEFG"
 
somestring.Right(3== L"EFG"
 
The index starts at 0 (MFC and C#), so nIndex=2 means the 3rd char!
cs


C#의 Substring()과 비교하면 이렇습니다.


두 언어의 문자열 자르기 비교표입니다.


MFC (CString)

C# (string)

 somestring.Left (nCount)

 somestring.Substring (0,nCount)

 somestring.Mid (nIndex)

 somestring.Substring (nIndex)

 somestring.Mid (nIndex,nCount)

 Somestring.Substring (nIndex,nCount)

 somestring.Right (nCount)

 somestring.Substring (somestring.Length-nCount,nCount)



C# Left (number of chars) 함수

문자열 자르기할 때, 맨 앞을 기준으로 원하는 숫자만큼의 char를 분리합니다.

아래 소스에선 "ABCDEFG" 중 "ABC"만 분리합니다.


1
2
3
4
// Sample: Extract the first 3 chars "ABC" from "ABCDEFG"
// Important: Don't forget to make sure the string is not empty or too short!
string somestring = "ABCDEFG";
string newstring = somestring.Substring(03);
cs

C# Right (number of chars) 함수

문자열 자르기할 때, 맨 뒤를 기준으로 원하는 숫자만큼의 char를 분리합니다. 아래 소스에선 "ABCDEFG" 중 "EFG"만 분리합니다.


1
2
3
4
// Sample: Extract the last 3 chars "EFG" from "ABCDEFG"
// Important: Don't forget to make sure the string is not empty or too short!
string somestring = "ABCDEFG";
string newstring = somestring.Substring(somestring.Length-33);
cs


C# Mid (index, number of chars) 함수

문자열 자르기할 때, 중간에서 원하는 char를 분리합니다. 아래 소스에선 "ABCDEFG" 중 "CDE"만 분리합니다.


1
2
3
4
// Sample: Extract 3 chars (starting at 'C') from "ABCDEFG" (nIndex=2 nCount=3)
// Important: Don't forget to make sure the string is not empty or too short!
string somestring = "ABCDEFG";
string newstring = somestring.Substring(23);
cs


C# Mid (index) 함수

문자열 자르기할 때, 중간에서 원하는 char를 분리합니다. index를 기준으로 문자열 끝까지를 분리합니다. 아래 소스에선 "ABCDEFG" 중 "CDEFG"만 분리합니다.


1
2
3
4
// Sample: Extract ALL chars (starting at 'C') from "ABCDEFG" (nIndex=2)
// Important: Don't forget to make sure the string is not empty or too short!
string somestring = "ABCDEFG";
string newstring = somestring.Substring(2);
cs



관련 글

C++ string Split

https://codingcoding.tistory.com/835


MFC, CString to Char*

https://codingcoding.tistory.com/574


C# Console.ReadLine 함수 사용법

https://codingcoding.tistory.com/334

댓글