How to remove whitespace within in a string in Flutter / dart and capitalize first letter of each word - flutter

I want to remove white space within a string like extra spaces in between the words. I have tried trim() method. but it only removes the leading and trailing whitespace I want to remove the spaces in between the string and I want to convert the first letter of the each word to capital .
example : var name = ' Aneesh devala ' to Aneesh Devala
I have tried this answers but it's not suitable for mine.

I hope this code will work for you.
String getCapitalizedName(String name) {
final names = name.split(' ');
String finalName = '';
for (var n in names) {
n.trim();
if (n.isNotEmpty) {
finalName += '${n[0].toUpperCase()}${n.substring(1)} ';
}
}
return finalName.trim();}

Related

flutter : how to substring string from last of special character like #

I have this string ffff #fgggg #, How can I do substring last #, Thats mean When I am using this code:
String text = `ffff #fgggg #`;
selectedUser = text.substring();
I expect selectedUser be == "";
I want to check text from last # ? If after last # be nothing, substring() method return "" ?
I found answer :
selectedUser = text.substring(text.lastIndexOf('#') + 1);
and I got "". It checked from last #.

Reliably Extracting String In Flutter List Every Time

So I have a quite interesting exercise I've been trying to solve for a while now, but haven't come up with a reliable solution, so I thought you guys could help me out. I have this String that is composed of few random custom parts, for example:
"William\nWilliam description here...\n$170.00 usd") + Uuid().v4();
I need to extract the part after '$' and '.', in this case 170, but it can be any number between.
UPDATE
as I said in the last comment, if I wanted to do it in a function (find the price only), it could go something like this:
deleteSumItem(item) {
final regEx = RegExp(r'\$\d+(?:\.\d+)?');
const textToSearch = r'item';
final priceValueMatch = regEx.firstMatch(textToSearch);
print(priceValueMatch.group(0));
_totalPrice.remove(priceValueMatch);
_counter = _counter - priceValueMatch; //getting error here to convert to num
//but int.parse won't work either, then I get the String error
//RegExp can't be assigned to paremeter String
}
Also, this function returns null for regex, so there is some mistake I'm making, any thoughts?
deleteSumItem(item) {
final regEx = RegExp(r'\1\d+(?:\.\d+)?');
final priceValueMatch = regEx.firstMatch(r'item');
print('THIS IS REGEX: $priceValueMatch');} //priceValueMatch returns null
fix
deleteSumItem(item) {
RegExp regExp = RegExp(r'\^(\d+)\^');
String input = item;
String match = regExp.firstMatch("r" + '"' + input + '"').group(1);
print('Match: $match');
int number = int.parse(match);
print('Number: $number');
_totalPrice.remove(number);
_counter = _counter - number;}
Assuming you can answer 'yes' to the questions in my above comment, you can simply use regular expressions to find the price value in your string:
final regEx = RegExp(r'\$\d+(?:\.\d+)?');
const textToSearch = r'William\nWilliam description here...\n$170.00 cm';
final priceValueMatch = regEx.firstMatch(textToSearch);
print(priceValueMatch.group(0)); // this will print $170.00
The regular expression is looking for a dollar sign \$ followed by 1 or more digits d+ followed by optional decimal point and optional digits behind that decimal (?:\.\d+)?.
This actually ignores a lot of the questions in my above comment. This simply looks for a price value preceded by a dollar sign within the string you give it.
Here is another approach based on your comments. This is assuming the new line characters will always exist
const textToSearch = 'William\nWilliam description here...\n170.00 cm';
final lines = textToSearch.split('\n'); // Split on new line character
// If your template is always the same,
// then your number will be at the start of line 3:
print(lines[2]); // Will print: 170.00 cm
// If you want just your 170 value then:
final regEx = RegExp(r'\d+');
final priceValueMatch = regEx.firstMatch(lines[2]);
final priceInt = int.parse(priceValueMatch.group(0));
print(priceInt); // Will print: 170

How to remove space at beginning and end of a string in Dart

using Dart How to remove space at the beginning and end of a string?
String name =" Well Wisher ";
or this
String name =" Well Wisher";
or from this
String name ="Well Wisher ";
Thanks #https://stackoverflow.com/users/5882307/omi-shah
the .trim() function is used to remove white space from the front and end of a string
String name =" Well Wisher ";
name.trim(); // "Well Wisher"

Remove Special Character from String in API

I have an API and it response me a string, that string contains a URLs of photos,
but the URLs contains special characters (backslash),
how can I remove that Special Character from String in API?
this is the String :
"photos":
"[\"uploads\\/products\\/photos\\/9UZGRucASnXoKsn6fnLqLcWYS7Ttb84JPOHOyJR1.jpeg\",\"uploads\\/products\\/photos\\/zjofjxnsjWus210f5S3YuijJMt9wXSnI6frgNRXc.jpeg\",\"uploads\\/products\\/photos\\/BA5oMG3EPVSDmQcLTYH3DZj1igwg4gMUvC6ItxmT.jpeg\",\"uploads\\/products\\/photos\\/gcCyQzli8M4ZGIWfcuU7DKf9C2y8rVxq5OpSHT9w.jpeg\",\"uploads\\/products\\/photos\\/1xPnp6ut6C7wJUlGHqUlnGrL6H3ClIPs0eFM96yc.jpeg\",\"uploads\\/products\\/photos\\/JjlVTcv6YencrYQClxCL2FRzMD6DUelfggtHVHbI.jpeg\",\"uploads\\/products\\/photos\\/b1iroJ8YWMj9PsTmSHML1iRlX3G8ayjYvhNP3bZO.jpeg\",\"uploads\\/products\\/photos\\/jUo1bUWi5bjEkk4c9WyQgGhUNGjqDYuFScildcq2.jpeg\"]",
When you have a string, you can pass in the url of the photo path in this case to a variable. Then you can use RegExp to replace all occurrences of a certain character in a String.
final myString = 'abc=';
final withoutEquals = myString.replaceAll(RegExp('='), ''); // abc
In your case:
String photoUrl = //Your photo Url
final cutPhotoUrl = photoUrl.replaceAll(RegExp('\'), ''); // Replaces all occurrences of backslash with blank whitespace
The idea really is to use the replaceAll method, but you must escape the backslash as it follows:
string.replaceAll(RegExp(r'\\'), '')
If you don't do so, you are actually escaping your last ` character and will get an error.

Flutter dart replaceAll special character not working

I cant get this to work it doesnt replace all the special character and white spaces?
String sentence = "!##$%ˆ&*()<>?:"{} Hello ";
String newSentence = sentence.replaceAll("\\W+","").toLowerCase();
print(newSentence);
Use
String sentense = '!#resu##me'.replaceAll(new RegExp('\\W+'), '');
print(sentense);
Output:
resume
For more reference https://api.dart.dev/stable/2.8.3/dart-core/String/replaceAll.html
You should try this:
String sentence = "!##\$%ˆ&*()<>?:\"{} Hello ";
String newSentence = sentence.replaceAll(RegExp(r'[^a-zA-Z0-9 ]'),"").toLowerCase();
print(newSentence);