1

please I want to know how to delete whitespaces from the end of string in dart . ex :

String name ="(  Sana .  Harun  )";

when I use this code

print(sana.replaceAll(new RegExp(r"\s+\b|\b\s"),""));

it just delete from the beggining and not from the end , and print Like this :(Sana.Harun ) .. there is a space between Harun and ) . I want to delete the space between Harun and )

Can any one help me , please ?

1
  • Try adding a + after the \b\s in the RegExp. As written, it only removes one space, not all the spaces after a word break. Commented Jan 17, 2020 at 6:26

2 Answers 2

3

Call this function/method !

String getWithoutSpaces(String s){
      String tmp = s.substring(1,s.length-1);
      while(tmp.startsWith(' ')){
       tmp = tmp.substring(1);
      }
      while(tmp.endsWith(' ')){
       tmp = tmp.substring(0,tmp.length-1);
      }

  return '('+tmp+')';
}

pic

Sign up to request clarification or add additional context in comments.

2 Comments

I used this method @Naveen Avidi .. and it works with me ... Thank you for your help ..... String validate_Strings(String name , String last) { String n =name.replaceAll(new RegExp(r"\s+\b|\b\s"),""); String f= n.replaceAll(" ", ""); String c= last.replaceAll(new RegExp(r"\s+\b|\b\s"),""); String l = c.replaceAll(" ", ""); String u = '('+f+'.'+l+')'; print(u); return u; }
If it's useful and helped you ! Please accept the answer.
1

I found the solution , is to use this code

print(sana.replaceAll(" ", ""));

so the space between Harun and ) disappear

2 Comments

Maybe you should look at stopping the spaces getting there in the first place. I am presuming they are there as a result of user input?
Yes , that's because the user input ,so ?i want when user enter something , the code delete every space between the letters , but I solved the problem now .. with this code .. String validate_Strings(String name , String last) { String n =name.replaceAll(new RegExp(r"\s+\b|\b\s"),""); String f= n.replaceAll(" ", ""); String c= last.replaceAll(new RegExp(r"\s+\b|\b\s"),""); String l = c.replaceAll(" ", ""); String u = '('+f+'.'+l+')'; print(u); return u; } @GrahamD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.