Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Created January 16, 2020 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blasanka/85b942a46e922bb50d05fbc0134c0c6e to your computer and use it in GitHub Desktop.
Save Blasanka/85b942a46e922bb50d05fbc0134c0c6e to your computer and use it in GitHub Desktop.
Different ways to replace string
void main() {
String str = "one.two";
print(str.replaceAll(".two", ""));
// or
print(str.split(".").first);// split() will split from . and gives new List with separated elements.
// or
print(str.substring(0, str.indexOf("."))); // substring from start character to firstly found dot
// or
String newStr = str.replaceRange(str.indexOf("."), str.length, "");
print(newStr);
// another example
String nums = "1,one.2,two.3,three.4,four";
List values = nums.split(".");
values.forEach(print);
//output
// 1,one
// 2,two
// 3,three
// 4,four
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment