Issue
How can I check whether a given string contains another substring more than once?
fun isFieldRepeated(jsonIn: String, field:String): Boolean {
var count: Int = 0;
while (jsonIn.indexOf(field) > -1) {
jsonIn = jsonIn.substring(jsonIn.indexOf(field)+field.length(),jsonIn.length());
count++;
}
if(count>1)
return true;
return false;
}
Solution
Are you sure that your code, albeit a bit redundant, doesn't work?
The following should do what you seemingly want it to, but is a bit cleaner :)
fun isFieldRepeated(jsonIn: String, field:String): Boolean {
val index: Int =jsonIn.indexOf(field)
return index!=-1 && jsonIn.indexOf(index, field)!=-1
}
Hope it helps!
Answered By - pentexnyx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.