how to replace lastindexof "," to the word "and" but avoid to replace "and" inside the open and close parenthesis in vb.net -


how replace last accurancy "," word "and" avoid replace "and" inside open , close parenthesis in vb.net

sample:

a,a1,a2,a3,b,b1,b2,b3,c,c1,c2,c3

i want output a(a1,a2,a3),b(b1,b2,b3) , c(c1,c2,c3)

this code: dim strng string = me.checkbox1.text

    'now find position of last appearing ","     dim comaposition integer     comaposition = strng.lastindexof(",") 'it 0  based      'if not found, return -1 , u can exit, no need work     if comaposition = "-1"          exit sub     end if      'remove comma     dim string_after_removing_comma string     string_after_removing_comma = strng.remove(comaposition, 1)      'add "and" in same position comma found     dim final_string string     final_string = string_after_removing_comma.insert(comaposition, " , ")      'show on textbox     checkbox1.text = final_string 

how can idea

here solution you:

dim test string = "a,a1,a2,a3,b,b1,b2,b3,c,c1,c2,c3"  'this gets a,b,c  dim singlechar string() = test.split(",").where(function(a) not a.any(function(c) char.isdigit(c))).toarray()  'this gets rest of string dim charwithnumbers string() = test.split(",").where(function(a) a.any(function(c) char.isdigit(c))).toarray()  dim strfinal string = ""  dim diction = new dictionary(of string, list(of string))() dim list = new list(of string)()  'this add similar strings found in "charwithnumbers" based on  'the characters in "singlechar" list each c char in singlechar     list = new list(of string)()     diction.add(c, list)     each item string in charwithnumbers         if item.contains(c)             list.add(item)         end if     next next  'grouping list each item  in diction     strfinal += item.key + "(" + string.join(",", item.value) + ")," next  strfinal = strfinal.trimend(","c) 'removes trailing ','  'to replace "," "and" dim lastchar = singlechar(singlechar.count() - 1)  strfinal = strfinal.replace("," + lastchar + "(", " , " + lastchar + "(")  console.writeline(strfinal) 

output:

a(a1,a2,a3),b(b1,b2,b3) , c(c1,c2,c3) 

this not optimized performance, don't think bother it.

check output in .net fiddle


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -