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"
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.
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.
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.
' 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.
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 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.
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.
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
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'
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);
}
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With