String ignore case kotlin

Kotlin program to check if a string contains another substring

In this kotlin programming tutorial, we will learn how to check if a substring exists in another string or not. The program will take the strings as input from the user and print out the result.

We will learn how to check if the string contains a substring by ignoring all case of each character during the checking and by not ignoring the case.

The first argument is the substring that we need to check.

Below examples will add more clarity to you :

Check substring in a string without ignoring case :

Let’s try to implement the contains method by not ignoring all character case :

fun main(args: Array)  val line: String val subStr: String print("Enter a string : ") line = readLine().toString() print("Enter a sub string : ") subStr = readLine().toString() if (line.contains(subStr, false))  print("String '$line' contains substring '$subStr'") > else  print("String '$line' doesn't contain substring '$subStr'") > >

kotlin check substring in string

The output of the above program will look like below :

: Hello World Enter a sub string : Hello String 'Hello World' contains substring 'Hello' Enter a string : Hello World Enter a sub string : hello String 'Hello World' doesn't contain substring 'hello' 

As you can see, if we are passing false to the contains method, it checks the character case while comparing.

Check substring in a string ignoring case :

For checking substring in a string ignoring case, the only thing we need to do is pass true instead of false as the above example.

fun main(args: Array)  val line: String val subStr: String print("Enter a string : ") line = readLine().toString() print("Enter a sub string : ") subStr = readLine().toString() if (line.contains(subStr, true))  print("String '$line' contains substring '$subStr'") > else  print("String '$line' doesn't contain substring '$subStr'") > >
: Hello World Enter a sub string : Hello String 'Hello World' contains substring 'Hello' Enter a string : Hello String Enter a sub string : hello String 'Hello String' contains substring 'hello' 

We have learned how to check if a substring exists in a string or not in Kotlin. Try to run both examples we have shown above and drop one comment below if you have any queries.

Источник

How to check if strings are equal ignoring case in Kotlin?

In this tutorial, you shall learn how to check if given strings are equal ignoring case in Kotlin, using String.equals() function, with examples.

Kotlin – Check if strings are equal ignoring case

To check if strings are equal ignoring case in Kotlin, you can use String.equals() function.

Call the equals() function on the first string and pass the second string, and ignoreCase=true as arguments to the function. The function returns a boolean value true if the two strings are equal ignoring the case of the characters, or else, it returns false.

Syntax

The syntax to check if the strings str1 and str2 are equal ignoring the case is

str1.equals(str2, ignoreCase=true)

Examples

In the following program, we take two strings in str1 and str2 , and check if these two strings are equal, or not, ignoring the case.

Two strings are equal ignoring case.

Reference tutorials for the program

Conclusion

In this Kotlin Tutorial, we learned how to check if two strings are equal ignoring case using String.equals() function.

  • How to Check if String Ends with Specified Character in Kotlin?
  • How to Check if String Ends with Specified String in Kotlin?
  • How to Check if String Starts with Specified Character in Kotlin?
  • How to Check if String Starts with Specified String Value in Kotlin?
  • How to Check if Two Strings are Equal in Kotlin?
  • How to Compare Strings in Kotlin?
  • How to Compare Strings in Kotlin?
  • How to Create an Empty String in Kotlin?
  • How to Filter Characters of String in Kotlin?
  • How to Filter List of Strings based on Length in Kotlin?
  • How to Filter only Non-Empty Strings of Kotlin List?
  • How to Filter only Strings from a Kotlin List?
  • How to Get Character at Specific Index of String in Kotlin?
  • How to Initialize String in Kotlin?
  • How to Iterate over Each Character in the String in Kotlin?
  • How to Remove First N Characters from String in Kotlin?
  • How to Remove Last N Characters from String in Kotlin?
  • How to check if a String contains Specified String in Kotlin?
  • How to create a Set of Strings in Kotlin?
  • How to define a List of Strings in Kotlin?
  • How to define a String Constant in Kotlin?
  • How to get Substring of a String in Kotlin?
  • Kotlin String Concatenation
  • Kotlin String Length
  • Kotlin String Operations
  • Kotlin String.capitalize() – Capitalize First Character
  • Kotlin – Split String to Lines – String.lines() function
  • Kotlin – Split String – Examples
  • Kotlin – String Replace – Examples
  • Kotlin – String to Integer – String.toInt()
  • [Solved] Kotlin Error: Null can not be a value of a non-null type String

Источник

Читайте также:  Google font css url
Оцените статью