1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng Lập trình C# 2010: Chương 6 - ĐH Đồng Nai Công nghệ Đồng Nai

74 25 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Bài Giảng Lập Trình C# 2010: Chương 6
Trường học Dong Nai University of Technology
Thể loại bài giảng
Thành phố Đồng Nai
Định dạng
Số trang 74
Dung lượng 480,89 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Bài giảng Lập trình C# 2010: Chương 6 - String Class có nội dung trình bày về: Constructor, String Field, String Operators, String Methods, DEMO String & Dictionary.

Trang 1

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 2

characters.

Trang 3

DONG NAI UNIVERSITY OF TECHNOLOGY

Constructor

There are many overload Constructors I would like to tell you the Constructor below:

String Constructor ( Char [])    

an array of Unicode characters

Trang 4

(new char[]{'H ','U ','I'});

M essageBox.Show (strH U I);

String strH U I = new String

(new char[]{'H ','U ','I'});

M essageBox.Show (strH U I);

Use string or String?

Use string or String?

Trang 5

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 7

DONG NAI UNIVERSITY OF TECHNOLOGY

String Properties

Chars Gets the character at a specified character

position in the current String object

Length Gets the number of characters in the

current String object

Trang 8

( string strA, string strB )Description

indicates their relative position in the sort order

-1 strA is less than strB

Trang 9

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 10

string strA, string strB,

bool ignoreCase )

Description

case, and returns an integer that indicates their relative position in

the sort order

Trang 11

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

(“A", “a”,true);nRet=0

(“a", “A”,false);nRet=-1

(“A", “a”,false);nRet=1

Trang 12

public int CompareTo( string strB )

Description

whether this instance precedes, follows, or appears in the same

position in the sort order as the specified String (0 ; 1 ; -1)

Trang 13

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 15

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public bool Contains( string value )

Description

occurs within this string

Return true or false

Trang 17

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 19

DONG NAI UNIVERSITY OF TECHNOLOGY

Determines whether the end of this string instance matches the

specified string when compared using the specified comparison

option

Trang 21

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public static string Format

(string format, params Object[] args )

Description

Replaces the format item in a specified string with the string

representation of a corresponding object in a specified array

Trang 22

strFormat = string.Format("Tỉ giá {0:d} = {1:c}", DateTime.Today,

22.3);

MessageBox.Show(strFormat);

Trang 23

HO CHI MINH UNIVERSITY OF INDUSTRY

Author: Duy Thanh Tran – Phone : 0987773061 - Email: thanhxeiko@gmail.com – Blog: http://thanhxeiko.wordpress.com

Trang 24

character in this string

or -1 if it is not

Trang 25

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "N guyễễ n Thị Long Lanh Chòng Chành Ánh Tuyễế t" ;

int nRet= str.IndexO f( 'T' );

Trang 26

public int IndexOf( string value )

Description

this instance

Trang 27

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Học! Học Nữa ! Học Mãi" ;

int nRet = str.IndexOf( "Học" );

Trang 28

Description

Reports the index of the first occurrence of the specified string in

to use for the specified string

Trang 29

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string strHUI = "Welcome C# 2010" ;

int nRet = strHUI.IndexOf( "WELCOME" ,

Trang 30

public int IndexOfAny( char[] anyOf )

Description

Reports the index of the first occurrence in this instance of any

character in a specified array of Unicode characters

Trang 31

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Không! không; có? có?" ;

int nRet=str.IndexOfAny( new char [] { '>' , '?' , ';' });

 nRet=12

Trang 32

string value )Description

Inserts a specified instance of String at a specified index position

in this instance

Trang 33

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 34

Reports the index position of the last occurrence of a specified

Unicode character within this instance

-1 if it is not

Trang 35

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 36

public int LastIndexOf( string value)

Description

Reports the index position of the last occurrence of a specified

string within this instance

The zero-based index position of value if that string is found, or -1 if it is not

If value is String.Empty  , the return value is the last index position in this instance

Trang 37

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Học ! Học Nữa ! Học Mãi";

int nRet = str.LastIndexOf("Học");

nRet =16

nRet = str.LastIndexO f("H Ọ C");

nRet = -1

Trang 38

comparisonType )

Description

Reports the index of the last occurrence of a specified string within

to use for the specified string

Trang 39

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Học ! Học Nữa ! Học Mãi" ;

int nRet = str.LastIndexOf( "HỌC" ,

StringComparison OrdinalIgnoreCase);

 nRet =16

Trang 40

public string Remove( int startIndex )

Description

Deletes all the characters from this string beginning at a specified

position and continuing through the last position

Trang 41

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 42

int count )Description

Deletes a specified number of characters from this instance

beginning at a specified position

Trang 43

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 44

char newChar )

Description

Returns a new string in which all occurrences of a specified

Unicode character in this instance are replaced with another

specified Unicode character

Trang 45

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 46

string newValue )

Description

Returns a new string in which all occurrences of a specified string

in the current instance are replaced with another specified string

Trang 47

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Phải xem tài liệu trước khi tới lớp, tài liệu này rất quan trọng!" ;

str = str.Replace( "tài liệu" , "document" );

str = "Phải xem document trước khi tới lớp, document này rất quan trọng!" ;

Trang 48

Returns a string array that contains the substrings in this instance

that are delimited by elements of a specified Unicode character

array

Trang 49

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Trần văn tèo;21 tuổi;Chưa vợ;Côn Đảo";

string []strArr=str.Split(new char[]{';'});

foreach (string s in strArr)

Trang 50

public string[] Split( string[] separator, StringSplitOptions options )

Description

Returns a string array that contains the substrings in this string

that are delimited by elements of a specified string array A

parameter specifies whether to return empty array elements

Trang 51

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Example

string str = "Trần văn tèo;;21 tuổi;Chưa vợ;Côn Đảo" ;

string []strArr=str.Split( new string [] { ";" }, StringSplitOptions.None);

M essageBox Show (strArr.Length+ "" );// 5

foreach ( string s in strArr)

Trang 52

string []strArr=str.Split( new string [] { ";" }, StringSplitOptions.R em oveEm ptyEntries);

M essageBox Show (strArr.Length+ "" );// 4

foreach ( string s in strArr)

Trang 53

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public bool StartsWith( string value )

Description

Determines whether the beginning of this string instance matches

the specified string

Trang 54

bool bStart=str.StartsWith( "<head>" );

Trang 55

DONG NAI UNIVERSITY OF TECHNOLOGY

Determines whether the beginning of this string instance matches

the specified string when compared using the specified

comparison option

Trang 56

bool bStart=str.StartsWith( "<HEAD>" ,

StringComparison Ordinal);

MessageBox Show(bStart.ToString());False

bool bStart=str.StartsWith( "<HEAD>" ,

StringComparison OrdinalIgnoreCase);

True

Trang 57

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public string Substring( int startIndex )

Description

Retrieves a substring from this instance The substring starts at a

specified character position

Trang 58

string strSub = str.Substring(5);

MessageBox Show(strSub);

Trang 59

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public string Substring( int startIndex, int length )

Description

Retrieves a substring from this instance The substring starts at a

specified character position and has a specified length

Trang 60

string strSub = str.Substring(10,13);

MessageBox Show(strSub);

strSub = str.Substring(0,9);

Trang 61

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 63

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 64

MessageBox Show(str);

 microsoft visual studio 2010

Trang 65

DONG NAI UNIVERSITY OF TECHNOLOGY

Trang 66

MessageBox Show(str);

M ICRO SO FT VISU AL STU D IO 2010

Trang 67

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public string Trim()

Description

Removes all leading and trailing white-space characters from the

current String object

Trang 68

MessageBox Show(str.Length+ "" ); //35

MessageBox Show(str);

 " Microsoft Visual Studio 2010 "

string str = " Microsoft Visual Studio 2010 " ;

Trang 69

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public string TrimEnd( params char[] trimChars )

Description

Removes all trailing occurrences of a set of characters specified in

Trang 70

str=str.TrimEnd(new char [] { ' ' , '?' , '!' , '#' });

MessageBox Show(str.Length+ "" );// 28

MessageBox Show(str);

 "Microsoft Visual Studio 2010"

Trang 71

DONG NAI UNIVERSITY OF TECHNOLOGY

String Methods

Name

public string TrimStart( params char[] trimChars )

Description

Removes all leading occurrences of a set of characters specified in

Trang 72

str=str.TrimStart(new char [] { ' ' , '?' , '!' , '#' });

MessageBox Show(str.Length+ "" );// 28

MessageBox Show(str);

 "Microsoft Visual Studio 2010"

Trang 73

DONG NAI UNIVERSITY OF TECHNOLOGY

DEMO String & Dictionary

DEMO String & Dictionary

Trang 74

END

Ngày đăng: 08/05/2021, 12:17

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w