Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add space after each fourth + sign

Tags:

c#

asp.net

I have such a text for example:

"A01+B02+C03+D04+E05+F06+G07+H08+I09+J10+K11+L12+M13+N14+O15+P16"

I would like to add a space after each fourth '+' sign.

This is because if the text is too long in the grid's cell in my page, then it's simply cut off. So I'm thinking to simply wrap the string before binding the data to the grid.

I've played around with several string methods, like getting the IndexOf and the adding a space with Insert, or using a StringBuilder to make a completely new string out of the original one, but I just can't get the final solution running.

Any help would be appreciated. Thanks.

like image 230
TheFitGeekGirl Avatar asked Nov 05 '10 16:11

TheFitGeekGirl


1 Answers

Use a regex:

Pattern pattern = Pattern.compile("([^+]*\\+){4}");
Matcher matcher = pattern.matcher(str);
matcher.replaceAll("\1 ");
like image 191
thejh Avatar answered Oct 20 '22 08:10

thejh