Kotlin java.lang.StringIndexOutOfBoundsException


线程“main”中的异常java.lang.StringIndexOutOfBoundsException:String index超出范围:2在java.lang.String.charAt(String.java:658)在demo.KottestKt.main(kottest.kt:42)package demo

fun main(args : Array<String>){ var m : Int val S1 : String val S2 : String print("give the first char:") S1= readLine()!!.toString() print("give the second char:") S2= readLine()!!.toString() var t=0 val len1=S1.length val len2=S2.length //comparing the two chars if (S1.length==S2.length){ for (i in S1.indices){ if (S1[i]!=S2[i]){ t=1 } } if (t==0){ println("the strings are the same") } else{ println("the strings are not the same") } } //checking if S2 is in S1 if (len1 > len2){ var k=0 for (j in S1.indices){ for (s in S2.indices){ if (S1[j]==S2[s]){ m=j var d=s for (d in S1.indices){ if (S1[d]!= S2[m]){ k=1 break } m++ } } } } if (k==0){ println("S2 is in S1") }else{ println("S2 is not in S1") } } }