Share
Go down
Admin
Tổng số bài gửi : 389
Points : 704
Reputation : 0
Join date : 11/10/2018
Xem lý lịch thành viênhttp://thanthan.forumvi.com

C# String Operations (Methods) Left, Mid, Right

on Fri Oct 19, 2018 12:31 pm
Left, Mid and Right are common used string operations in MFC (CString class).

MFC (CString) Samples:
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!

In C# we use Substring instead of Left, Mid and Right:


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)
Admin
Tổng số bài gửi : 389
Points : 704
Reputation : 0
Join date : 11/10/2018
Xem lý lịch thành viênhttp://thanthan.forumvi.com

Re: C# String Operations (Methods) Left, Mid, Right

on Fri Oct 19, 2018 12:39 pm
String Left (number of chars) in C#

Extract the first nCount characters (leftmost) from a string:


Code:
/* 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(0, 3);


Được sửa bởi Admin ngày Fri Oct 19, 2018 12:41 pm; sửa lần 1.
Admin
Tổng số bài gửi : 389
Points : 704
Reputation : 0
Join date : 11/10/2018
Xem lý lịch thành viênhttp://thanthan.forumvi.com

Re: C# String Operations (Methods) Left, Mid, Right

on Fri Oct 19, 2018 12:39 pm
String Right (number of chars) in C#

Extract the last nCount characters (rightmost) from a string:


Code:
/* 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-3, 3);


Được sửa bởi Admin ngày Fri Oct 19, 2018 12:41 pm; sửa lần 2.
Admin
Tổng số bài gửi : 389
Points : 704
Reputation : 0
Join date : 11/10/2018
Xem lý lịch thành viênhttp://thanthan.forumvi.com

Re: C# String Operations (Methods) Left, Mid, Right

on Fri Oct 19, 2018 12:40 pm
String Mid (index, number of chars) in C#

Extract nCount characters starting at nIndex from a string:


Code:
/* 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(2, 3);
Admin
Tổng số bài gửi : 389
Points : 704
Reputation : 0
Join date : 11/10/2018
Xem lý lịch thành viênhttp://thanthan.forumvi.com

Re: C# String Operations (Methods) Left, Mid, Right

on Fri Oct 19, 2018 12:43 pm
String Mid (index) in C#

Extract ALL characters starting at nIndex from a string:


Code:
/* 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);
Sponsored content

Re: C# String Operations (Methods) Left, Mid, Right

Về Đầu Trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết