Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I check whether a given string contains another substring?

How can I check whether a given string contains another substring?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading