Kotlin String endsWith Function

The endsWith function in Kotlin is used to check if a string ends with a specified suffix. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to perform suffix matching within a string.

Table of Contents

  1. Introduction
  2. endsWith Function Syntax
  3. Understanding endsWith
  4. Examples
    • Basic Usage
    • Using endsWith with Ignore Case
    • Checking for Multiple Suffixes
  5. Real-World Use Case
  6. Conclusion

Introduction

The endsWith function checks if a string ends with a specified suffix. This is useful for various string operations such as validation, formatting, and file type checking.

endsWith Function Syntax

The syntax for the endsWith function is as follows:

fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean fun String.endsWith(suffix: Char, ignoreCase: Boolean = false): Boolean 

Parameters:

  • suffix: The suffix to check for at the end of the string.
  • ignoreCase: If true, the search will ignore case differences (default is false).

Returns:

  • true if the string ends with the specified suffix, false otherwise.

Understanding endsWith

The endsWith function checks if the string ends with the given suffix. It can perform a case-sensitive or case-insensitive check based on the ignoreCase parameter.

Examples

Basic Usage

To demonstrate the basic usage of endsWith, we will create a string and check if it ends with a specific suffix.

Example

fun main() { val text = "Hello, World!" val endsWithExclamation = text.endsWith("!") val endsWithWorld = text.endsWith("World!") println("Does the text end with '!'? $endsWithExclamation") println("Does the text end with 'World!'? $endsWithWorld") } 

Output:

Does the text end with '!'? true Does the text end with 'World!'? true 

Using endsWith with Ignore Case

This example shows how to use the ignoreCase parameter for case-insensitive suffix checking.

Example

fun main() { val text = "Hello, World!" val endsWithWorldIgnoreCase = text.endsWith("world!", ignoreCase = true) println("Does the text end with 'world!' (ignore case)? $endsWithWorldIgnoreCase") } 

Output:

Does the text end with 'world!' (ignore case)? true 

Checking for Multiple Suffixes

This example demonstrates how to check for multiple possible suffixes using a combination of endsWith and other functions.

Example

fun main() { val fileName = "document.txt" val endsWithTxt = fileName.endsWith(".txt") val endsWithDoc = fileName.endsWith(".doc") println("Does the file name end with '.txt'? $endsWithTxt") println("Does the file name end with '.doc'? $endsWithDoc") } 

Output:

Does the file name end with '.txt'? true Does the file name end with '.doc'? false 

Real-World Use Case

Validating File Extensions

In real-world applications, the endsWith function can be used to validate file extensions, ensuring that files have the correct format.

Example

fun main() { val fileName = "report.pdf" if (fileName.endsWith(".pdf")) { println("The file is a PDF document.") } else if (fileName.endsWith(".docx")) { println("The file is a Word document.") } else { println("Unknown file format.") } } 

Output:

The file is a PDF document. 

Conclusion

The endsWith function in Kotlin’s String class is a convenient method for checking if a string ends with a specified suffix. It provides a simple way to perform suffix matching for various use cases, including validation, formatting, and file type checking. By understanding and using this function, you can effectively manage suffix matching operations in your Kotlin applications.

Leave a Comment

Scroll to Top