phongyewtong
phongyewtong

Reputation: 5319

Flutter dart replaceAll special character not working

I cant get this to work it doesnt replace all the special character and white spaces?

String sentence = "!@#$%ˆ&*()<>?:"{} Hello ";
String newSentence = sentence.replaceAll("\\W+","").toLowerCase();
print(newSentence);

Upvotes: 1

Views: 1762

Answers (2)

Steve
Steve

Reputation: 953

You should try this:

String sentence = "!@#\$%ˆ&*()<>?:\"{} Hello ";
String newSentence = sentence.replaceAll(RegExp(r'[^a-zA-Z0-9 ]'),"").toLowerCase();
print(newSentence);

Upvotes: 1

Jitesh Mohite
Jitesh Mohite

Reputation: 34280

Use

String sentense = '!@resu##me'.replaceAll(new RegExp('\\W+'), '');  
print(sentense);

Output:

resume

For more reference https://api.dart.dev/stable/2.8.3/dart-core/String/replaceAll.html

Upvotes: 3

Related Questions