How do i Replace every n-th character x from an String

Tolga Yasar

My Question would be how can replace every 3rd ';' from a String a put a ',' at this position ?

for eg.:

String s = "RED;34;34;BLUE;44;44;GREEN;8;8;BLUE;53;53"

so that the String looks like:

RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53

I tried to solve it like this but i can't take a charAt(i) and replace it with an other char.

int counter =0;
for (int i=0;i<s.length();i++){
    if(s.charAt(i) == ';'){

    counter++;
    }
    if(counter ==3){
    s.charAt(i)=',';
    counter =0;
    }


}
Joop Eggen

Normally some own effort is demanded from the question, but regex is hard.

s = s.replaceAll("([^;]*;[^;]*;[^;]*);", "$1,");

A sequence of 0 or more of not-semicolon followed by semicolon and such.

[^ ...characters... ] is some char not listed.

...* is zero or more of the immediately preceding match.

The match of the 1st group (...) is given in $1, so actually only the last semicolon is replaced by a comma.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to replace the n th occurance of a character in a String ?

How do we insert \n every n-character or/and after n-th space in a string in R?

How do you split a string at every n-th character in Swift?

How to replace every x-th occurence of a string in a text with RegEx?

How do I remove/replace the ⁿ character from a string?

Splitting a string at every n-th character

How do I do something every n-th attempt?

How to replace every character from input? (python3.x)

How to replace every 4th character of a string using .gsub in Ruby?

How do I replace nth character in a String with a new character?

how to remove N-th character from string in R?

How do I remove every third character from a string in c# If I don't know what character it is?

Replace every nth character from a string

How do I mask the input with every key press as a character from a predefined string in an html input field?

Python: How do I reverse every character in a string list?

How do I apply the for-each loop to every character in a String?

How do I replace/update all instances of every string in a column with matching values from another table?

add conditional character every i-th place in a string

Split a string at every n-th character in JavaScript

How can I remove every nth character from a string in javascript?

How to subtract every nth value in a column from every (n-x)th value?

How to insert a character every n characters from end of string

How can I replace a literal \n with a newline character? (on OS X)

How do I replace characters in a string using a character map?

How do I replace the last occurrence of a character in a string using sed?

How do I replace all special characters in a string with the escape character?

How do I replace two or more instances of a character in an Excel string?

Replace n-th tab character with a string using Javascript/jQuery

How do I make cron run something every "N"th minute, where n % 5 == 1?