vb.net - Get next string following array of char -
i have array of chars, random except every character occurs @ once.
i have string containing characters present in array. want string "counted upwards" (if makes sense), "111" becomes "112", or "aaa" becomes "aab".
let´s array of chars contains 1, 2, , 3. example above works, when string "333" (should become "1111"), function returns empty - or wrong - string.
if call function again, providing wrong string, after few times returns correct value ("1111").
why happen?
this function:
public function getnextstring(byval currentstr string, byval pattern() char) string 'currentstr string want count upwards, pattern() array of chars dim nextstr string = "" dim currentstrarray() char = currentstr.tochararray dim currenstrposition integer = currentstrarray.length - 1 dim finished boolean = false until finished = true dim newposition integer = getpositioninarray(currentstrarray(currentstrposition)) 'this custom function, should self-explaining if newposition = nothing return nothing newposition += 1 try currentstrarray(currenstrposition) = pattern(newposition) finished = true catch ex indexoutofrangeexception currentstrarray(currentstrposition) = pattern(0) currentstrposition -= 1 end try if currentstrposition < 0 nextstr = pattern(0) finished = true end if loop integer = 0 currentstrarray.length - 1 nextstr = nextstr & currentstrarray(i) next return nextstr end function any ideas?
edit:
as example, have array {"1","2","3"}. string first "111". want test these strings hashsums. after testing string, need next string, "112". then, "113", "121", "122", , on. when string reaches "333" , string not 1 i'm looking for, no string 3 characters (all 3-character-combinations possible array have been tried). need start again using 4 characters. that´s why "333" supposed become "1111". hope helps.
edit #2:
i found error. redimensioned array incorrectly, last index empty. made strings weird. thank working solutions, have day!
well, convert number, add 1 , convert string.
convert string number:
function convertstringtonumber(input string, pattern char()) integer dim number integer = 0 dim chardigits = pattern.tolist() dim numberbase = chardigits.count var = 0 input.length - 1 dim digit = chardigits.indexof(input(input.length - 1 - i)) if digit <> -1 number += digit * cint(math.pow(numberbase, i)) end if next return number end function convert number string:
function convertnumbertostring(number integer, pattern char()) string dim chardigits = pattern.tolist() dim numberbase = chardigits.count dim buffer = new stack(of char)() 'var j = buffer.length; while number > 0 buffer.push(chardigits(number mod numberbase)) number = number / numberbase end while dim sb = new stringbuilder() while buffer.count > 0 sb.append(buffer.pop()) end while return sb.tostring() end function now have conversion functions, have this:
function getnextstring(byval currentstr string, byval pattern() char) string dim number = convertstringtonumber(currentstr, pattern) number += 1 return convertnumbertostring(number, pattern) end function
Comments
Post a Comment