Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the last character(s) from String in Dart [duplicate]

Tags:

dart

This is what I came up with. If there is a better way, let me know.

like image 450
live-love Avatar asked May 18 '20 19:05

live-love


People also ask

How do you remove the last character of 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 remove the first and last character from a string Dart?

substring(2, error. length()-1)) to remove first and last brackets!

How do I remove the last 3 characters from a string?

slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.

How to remove the last character of a string using it?

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. For example, if the string is hello and if we pass the start index as 0 and end index as 4, it will return hell.

How to remove special characters from string in dart or flutter?

We will use RegEx expression to remove special characters from String in Dart or Flutter. How to Remove Special Characters and Get Alphabets and Numbers? Here, we have string "str" with special characters, alphabets, numbers. We have used the replaceAll () method on string with RegEx expression to remove the special characters.

How to get one substring from a string in Dart?

Dart provides one method called substring that returns one substring from a string. This method is defined as below : Here, startIndex is the starting index of the string that we will get the substring from. endIndex is optional. If we don’t provide it, it will take the substring to the end of the given string.

How do I remove a character from a StringBuffer in Java?

The StringBuffer class provides a method deleteCharAt (). The method deletes a character from the specified position. We use the method to remove a character from a string in Java.


2 Answers

Remove last character:

if (str != null && str.length > 0) {
  str = str.substring(0, str.length - 1);
}

Remove last 5 characters:

if (str != null && str.length >= 5) {
  str = str.substring(0, str.length - 5);
}
like image 174
live-love Avatar answered Oct 07 '22 12:10

live-love


The answer that you have given would suffice the problem, just wanted to share this another way to remove the last element. Here I am using removeLast function provided by the dart library.

This function can be used on any list to remove the last element.

void main() {
  String x = "aaabcd";
  List<String> c = x.split(""); // ['a', 'a', 'a', 'b', 'c', 'd']
  c.removeLast(); // ['a', 'a', 'a', 'b', 'c']
  print(c.join()); //aaabc
}
like image 1
Yudhishthir Singh Avatar answered Oct 07 '22 11:10

Yudhishthir Singh