Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

string - 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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...