kotlin 指定した文字列が後ろにあるかを判定する
kotlinで、指定した文字列が後ろにあるかを判定する手順を記述してます。「endsWith」を使用します。
環境
- OS windows11 home
- java 19.0.1
- kotlin 1.7.20-release-201
手順
指定した文字列が後ろにあるかを判定するには、「endsWith」で可能です。
文字列.endsWith(文字列) // あれば true なければ false実際に、使用してみます。
fun main() {
var str = "abcde"
println( str.endsWith("e") ) // true
println( str.endsWith("de") ) // true
println( str.endsWith("cd") ) // false
str = "あいうえお"
println( str.endsWith("お") ) // true
println( str.endsWith("えお") ) // true
println( str.endsWith("うえ") ) // false
}判定されていることが確認できます。
また、「空白」があっても正しく判定されます。
fun main() {
var str = " abc "
println( str.endsWith (" ") ) // true
println( str.endsWith ("c ") ) // true
println( str.endsWith ("c") ) // false
str = " あいう "
println( str.endsWith (" ") ) // true
println( str.endsWith ("う ") ) // true
println( str.endsWith ("う") ) // false
}サロゲートペア文字
通常の2バイトで1文字で表すところを、4バイトで1文字となるサロゲートペア文字でも正しく判定されます。
fun main() {
var str = "🙍😨😪🙈😛😼"
println( str.endsWith("😼") ) // true
println( str.endsWith("😛😼") ) // true
println( str.endsWith("😪🙈") ) // false
}-
前の記事
MySQLのエラー『エラー1451: Cannot Delete or Update a Parent Row』の解決方法 2025.08.20
-
次の記事
PostgreSQLでの『ERROR: prepared statement already exists』の原因と対処法 2025.08.21
コメントを書く