How can I convert '..' to '.'?

I have a text

String text = "abcd.."

and

String text2 = "abcd...."

I want to write a function and output "abcd."

like

function(String string){
//codes

return string // string must be "abcd." here
}

Upvotes: 1

Views: 55

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

If all occurrences of 2 or more dots should be replaced, you could use replaceAll with a pattern \.{2,} to match 2 or more dots and replace with a single dot.

String stripDots(String string){
    return string.replaceAll(RegExp(r'\.{2,}'), '.');
}

String text = "abc.... Def..";
print(stripDots(text));

Output

abc. Def.

Dart demo

Upvotes: 3

Related Questions