C#详解-Contains、StartsWith、EndsWith、Indexof、lastdexof

Table of contents

Introduction:

process:

Example 1.1

Example 1.2​

Summarize:


Introduction:

In C#, Contains, StarsWith, EndWith, and IndexOf are all string functions.
1. The Contains function is used to determine whether a string contains a specified substring and returns a Boolean value (True or False).
2. The StartsWith function is used to determine whether a string starts with a specified substring and returns a Boolean value (True or False).
3. The EndsWith function is used to determine whether a string ends with a specified substring and returns a Boolean value (True or False).
4.Indexof: is a string or array method used to find the index position of a specified element or character in a string or array. This method returns the index position of the first match, or -1 if no match is found.
5.lastIndexof: Find the index of the last string.

process:

Example 1.1

The purpose of Contains, StartsWith and Endswith is to make strings more convenient, efficient and readable, so two of them are commented out, taking Contains as an example.
advantage:

-Convenient to quickly determine whether a string contains a specific substring

-These methods can help us better control and filter input when processing strings

-Improve program performance and efficiency

-The code is more concise and readable

Example 1.2 

The Indexof and lastIndexof methods are the same, except that lastIndexof finds the index of the last string, so comment lastIndexof as an example Indexfo.

advantage:

-Quickly find the index position of an element

- Check if the element exists

- Determine duplicate elements

-Array deduplication

Summarize:

Contains, StarsWith, and EndWith can all be used to search and match strings to determine whether specific parts of the string meet the requirements. The common points are that they all receive a substring as a parameter and return a Boolean value to indicate the matching result. The difference between them is that the search scope is different. Contains searches for the entire string, StartsWiht searches for the beginning, and EndsWiht searches for the end.

Guess you like

Origin blog.csdn.net/weixin_59272777/article/details/132432140