Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trim and remove specific characters from beginning and end of a string in dart

Tags:

flutter

dart

I am looking for a Dart function similar to python's strip function. Remove specific characters only from beginning and end.

String str = "^&%.      , !@  Hello @World   , *%()@#$ "
String newStr = str.strip("#*)(@!,^&%.$ ");

Result:

"Hello @World"
like image 928
jerrymouse Avatar asked Aug 14 '20 09:08

jerrymouse


People also ask

How do you remove special characters from a string in darts?

We have used the replaceAll() method on string with RegEx expression to remove the special characters. It will give you the alphabets and numbers both and Special characters will be removed.

How do you remove the first and last character from a string in darts?

Method 1: Using substring : It returns the substring starting from startIndex inclusive and ending at endIndex exclusive. We can remove the last character of a string using this method by providing the start index as 0 and end index as string-length - 1.

How do you trim string darts?

Dart – Trim String To trim leading and trailing spaces or white space characters of a given string in Dart, you can use trim() method of String class. String. trim() returns a new string with all the leading and trailing white spaces of this string removed.

How do you remove the string after a certain character in flutter?

' in the String it will use the first occurrance. If you need to use the last one (to get rid of a file extension, for example) change indexOf to lastIndexOf . If you are unsure there is at least one occurrance, you should also add some validation to avoid triggering an exception. if you want to remove only one.

How to trim a string in Dart?

Following are the methods used to trim a string in Dart: This method removes the white spaces from both sides of a string. As the string is immutable in dart, it can’t modify the original string.

How to trim a specific character from the start and end?

How to Trim a Specific Character from the Start and End of a String using JavaScript? Regular Expression (RegEx) is the easiest way to trim a specific character from a string in JavaScript. Use the JavaScript replace () method with RegEx to remove a specific character from the string.

How do I trim leading and trailing characters from a string?

The TRIM function allows you to trim leading and/or trailing characters from a string. The following shows the syntax of the TRIM function. First, specify the trim_character, which is the character that the TRIM function will remove.

How do you trim a string in SQL?

If you specify TRAILING, the TRIM function will remove any trailing characters that match the trim_character. If you specify BOTH or none of three, the TRIM function will remove both leading and trailing characters that match the trim_characters. The TRIM function returns NULL if either trim_character or source string is NULL. SQL TRIM examples


Video Answer


4 Answers

You can use a regex to find the length of the leading and trailing part of the string that contain symbols. Then you can make a substring using the indices:

String str = "^&%.  ^    , !@  Hello@ World   , *%()@#\$ ";
RegExp regex = new RegExp(r'[#*)(@!,^&%.$\s]+');
int leftIdx = regex.stringMatch(str).length;
int rightIdx = regex.stringMatch(str.substring(leftIdx).split('').reversed.join('')).length;
String newStr = str.substring(leftIdx, str.length - rightIdx);

Result:

'Hello@ World'
like image 64
Mobina Avatar answered Nov 10 '22 23:11

Mobina


You can write an extension on String like this

void main() {
  String str = "^&%.      , !@  Hello @World   , *%()@#\$ ";
  String newStr = str.strip("#*)(@!,^&%.\$ ");                //<== Hello @World
}

extension PowerString on String {
  
  String strip(String whatToStrip){
    int startIndex, endIndex;
    
    for(int i = 0; i <= this.length; i++){
      if(i == this.length){
        return '';
      }
      if(!whatToStrip.contains(this[i])){
        startIndex = i;
        break;
      }
    }
    
    for(int i = this.length - 1; i >= 0; i--){
      if(!whatToStrip.contains(this[i])){
        endIndex = i;
        break;
      }
    }
    
    return this.substring(startIndex, endIndex + 1);
  }
  
}
like image 31
Jigar Patel Avatar answered Nov 11 '22 00:11

Jigar Patel


This following strip function should remove the special characters as provided.

String strip(String str, String charactersToRemove){
  String escapedChars = RegExp.escape(charactersToRemove);
  RegExp regex = new RegExp(r"^["+escapedChars+r"]+|["+escapedChars+r']+$');
  String newStr = str.replaceAll(regex, '').trim();
  return newStr;
}

void main() {
  String str = r"^&%.      , !@  Hello @World   , *%()@#$ ";
  String charactersToRemove = r"#*)(@!,^&%.$ ";
  print(strip(str, charactersToRemove));
}

Result:

Hello @World

PS

Raw string can be created by prepending an ‘r’ to a string. It treats $ as a literal character.

like image 41
jerrymouse Avatar answered Nov 10 '22 23:11

jerrymouse


If you want to remove whitespaces and special characters , anywhere from the string then try this,

String name = '4 Apple 1 k g @@ @ price50';  
print(name.replaceAll(new RegExp(r'[#*)(@!,^&%.$\s]+'), ""));

Output:- 4Apple1kgprice50
like image 35
Ronak Patel Avatar answered Nov 10 '22 23:11

Ronak Patel