Created
January 2, 2019 16:19
-
-
Save photizzo/65cf36425166f2dfbef0d815fadb88d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val pattern = "###-##-#-####" | |
val count = pattern.length - pattern.replace("-", "").length | |
val patternsLength = mutableListOf<Int>() | |
var index = pattern.indexOf('-') | |
var length = pattern.substring(0,index).length | |
for(i in 0..count){ | |
patternsLength.add(length) | |
length = index+1 | |
try { | |
index = pattern.indexOf('-', index+1) | |
length = pattern.substring(length, index).length | |
} catch (e: StringIndexOutOfBoundsException) { | |
length = pattern.substring(length, pattern.length).length | |
} | |
} | |
println(patternsLength) | |
var string = "1234566" | |
var subStringList = mutableListOf<String>() | |
var beginIndex = 0 | |
var endIndex = patternsLength[1] | |
for (i in 1..patternsLength.size){ | |
if(endIndex > string.length){ | |
subStringList.add(string.substring(beginIndex, string.length)) | |
break | |
} | |
if(string.length > endIndex){ | |
subStringList.add(string.substring(beginIndex, endIndex)) | |
} | |
beginIndex = endIndex | |
endIndex += patternsLength[i] | |
} | |
println(subStringList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment