delete whitespace from the end of string in dart

1,212

Solution 1

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

Solution 2

I found the solution , is to use this code

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

so the space between Harun and ) disappear

Share:
1,212
Sana'a Al-ahdal
Author by

Sana'a Al-ahdal

Trying to develop myself every day, do some code, read a book, and eat healthy food 😉 Google Developer Profile

Updated on December 17, 2022

Comments

  • Sana'a Al-ahdal
    Sana'a Al-ahdal over 1 year

    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 ?

    • lrn
      lrn about 4 years
      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.
  • GrahamD
    GrahamD about 4 years
    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?
  • Sana'a Al-ahdal
    Sana'a Al-ahdal about 4 years
    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
  • Sana'a Al-ahdal
    Sana'a Al-ahdal about 4 years
    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; }
  • Naveen Avidi
    Naveen Avidi about 4 years
    If it's useful and helped you ! Please accept the answer.