Hàm endswith() trong Python xác định xem nếu chuỗi string hoặc chuỗi con đã cho của string (nếu bạn cung cấp chỉ mục bắt đầu beg và chỉ mục kết thúc end) kết thúc với hậu tố suffix thì trả về true, nếu không thì phương thức này trả về false. Nội dung chính - Syntax of String endswith()
- endswith() Parameters
- Return Value from endswith()
- Example 1: endswith() Without start and end Parameters
- Example 2: endswith() With start and end Parameters
- Passing Tuple to endswith()
- Example 3: endswith() With
Tuple Suffix
- Phương thức chuỗi "endWith" là gì?
Cú phápstr.endswith(suffix[, start[, end]])
Các tham số:
str1 = "Vi du ham endswith trong python...test";
suffix = "test";
print (str1.endswith(suffix))
print (str1.endswith(suffix, 20))
suffix = "ham"
print (str1.endswith(suffix, 2, 4))
print (str1.endswith(suffix, 2, 6))
Kết quả là:
In this tutorial, we will learn about the Python String endswith() method with the help of examples. The endswith() method returns True if a string ends with the specified suffix. If not, it returns False . Examplemessage = 'Python is fun'
# check if the message ends with fun
print(message.endswith('fun'))
# Output: True
Syntax of String endswith()The syntax of endswith() is: str.endswith(suffix[, start[, end]])
endswith() ParametersThe endswith() takes three parameters: - suffix - String
or tuple of suffixes to be checked
- start (optional) - Beginning position where suffix is to be checked within the string.
- end (optional) - Ending position where suffix is to be checked within the string.
Return Value from endswith()The endswith() method returns a boolean. - It returns True if a string ends with the specified suffix.
- It
returns False if a string doesn't end with the specified suffix.
Example 1: endswith() Without start and end Parameterstext = "Python is easy to learn."
result = text.endswith('to learn')
# returns False
print(result)
result = text.endswith('to learn.')
# returns True
print(result)
result = text.endswith('Python is easy to learn.')
# returns True
print(result)
Output False
True
True
Example 2: endswith() With start and end Parameterstext = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith('learn.', 7)
print(result)
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith('is', 7, 26)
# Returns False
print(result)
result = text.endswith('easy', 7, 26)
# returns True
print(result)
Output True
False
True
Passing Tuple to endswith()It's possible to pass
a tuple suffix to the endswith() method in Python. If the string ends with any item of the tuple, endswith() returns True. If not, it returns False
Example 3: endswith() With Tuple Suffixtext = "programming is easy"
result = text.endswith(('programming', 'python'))
# prints False
print(result)
result = text.endswith(('python', 'easy', 'java'))
#prints True
print(result)
# With start and end parameter
# 'programming is' string is checked
result = text.endswith(('is', 'an'), 0, 14)
# prints True
print(result)
Output False
True
True
If you need to check if a string starts with the specified prefix, you can use startswith() method in Python.
Phương thức chuỗi "endWith" là gì?Phương thức Java endWith được sử dụng để kiểm tra xem chuỗi có kết thúc với chuỗi con do người dùng chỉ định hay không. Dựa trên sự so sánh này, nó sẽ trả về boolean Đúng hoặc
Sai. Cú pháp public endsWith(suffix) Thông số suffix - Đây là một hậu tố. Giá trị trả về - False: Chuỗi ký tự được cung cấp trong "hậu tố" KHÔNG khớp với chuỗi kết thúc của chuỗi gọi
- True: Chuỗi ký tự được cung cấp trong "hậu tố" khớp với chuỗi kết thúc của chuỗi gọi
Exception
Không có Thí dụ: public class StringEx1 {
public static void main(String[] args) {
String str_Sample = "Java String endsWith example";
//Check if ends with a particular sequence
System.out.println("EndsWith character 'e': " + str_Sample.endsWith("e"));
System.out.println("EndsWith character 'ple': " + str_Sample.endsWith("ple"));
System.out.println("EndsWith character 'Java': " + str_Sample.endsWith("Java"));
}
} Đầu ra: EndsWith character 'e': true EndsWith character 'ple': true EndsWith character 'Java': false Java.lang.String.endsWith () trả về true nếu chuỗi này kết thúc với hậu tố đã chỉ định.
|