- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin String - endsWith() Function
The Kotlin string endsWith() function is used to determine whether a string or character sequence ends with a specified suffix or character. The function supports case-sensitive and case-insensitive comparisons, so it can be used to verify text, analyze text or filter text.
Syntax
Following is the syntax of the Kotlin string endsWith() function −
fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean fun String.endsWith(suffix: Char, ignoreCase: Boolean = false): Boolean fun CharSequence.endsWith(suffix: CharSequence, ignoreCase: Boolean = false): Boolean
Parameters
This function accepts following parameters −
- suffix: The string, character, or character sequence to check for at the end of the string.
- ignoreCase (optional): If true, the case is ignored during the comparison. Default is false.
Return value
This function returns boolean value, true if the string ends with the specified suffix; otherwise, false.
Example 1: Check String with Suffix
Following is the basic example, here we use endsWith() function to check string with specified suffix in kotlin −
fun main() {
val text = "Hello, Kotlin!"
println(text.endsWith("Kotlin!"))
// case sensitive
println(text.endsWith("kotlin!"))
}
Output
Following is the output −
true false
Example 2: If the String is Case Insensitive
Now, let's look at another example of the EndsWith() function; If the string is case insensitive, return false unless ignoreCase is specified. If ignoreCase is true, it returns true −
fun main() {
val text = "Hello, Kotlin!"
println(text.endsWith("kotlin!", ignoreCase = true))
}
Output
Following is the output −
true
Example 3: Check a String With Char Suffix
If we check a string with a character suffix by using theendsWith()function in Kotlin if the character is available at the last, it returns true; otherwise, it returns false &mius;
fun main() {
val string = "Hello, Kotlin"
println(string.endsWith("n"))
println(string.endsWith("!"))
}
Output
Following is the output −
true false