Created
January 16, 2020 06:23
-
-
Save Blasanka/85b942a46e922bb50d05fbc0134c0c6e to your computer and use it in GitHub Desktop.
Different ways to replace string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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