Flutter get first word - flutter

I'm trying to get the first word but I can't seem to find a way. I've tried using split like so:
Text("${contactList[index].userName!.split(" ")}, ")
But the result is an array like so:
[aufa, taf]
Any solutions?

according to the results you should write:
Text("${contactList[index].userName!.split(" ")[0]}, ")
instead of :
Text("${contactList[index].userName!.split(" ")}, ")

Try below code hope its help to you.
Text("${contactList[index].userName!.split(" ").elementAt(0)"),
Simple Example:
void main() {
String sampleText = "aufa taf";
var first = sampleText.split(" ").elementAt(0); // aufa
var second = sampleText.split(" ").elementAt(1);//taf
print(first);
}

Try this:
Text(
"${contactList[index].userName!.split(" ")[0]}, ",
),

Related

String transformation for subject course code for Dart/Flutter

For interaction with an API, I need to pass the course code in <string><space><number> format. For example, MCTE 2333, CCUB 3621, BTE 1021.
Yes, the text part can be 3 or 4 letters.
Most users enter the code without the space, eg: MCTE2333. But that causes error to the API. So how can I add a space between string and numbers so that it follows the correct format.
You can achieve the desired behaviour by using regular expressions:
void main() {
String a = "MCTE2333";
String aStr = a.replaceAll(RegExp(r'[^0-9]'), ''); //extract the number
String bStr = a.replaceAll(RegExp(r'[^A-Za-z]'), ''); //extract the character
print("$bStr $aStr"); //MCTE 2333
}
Note: This will produce the same result, regardless of how many whitespaces your user enters between the characters and numbers.
Try this.You have to give two texfields. One is for name i.e; MCTE and one is for numbers i.e; 1021. (for this textfield you have to change keyboard type only number).
After that you can join those string with space between them and send to your DB.
It's just like hack but it will work.
Scrolling down the course codes list, I noticed some unusual formatting.
Example: TQB 1001E, TQB 1001E etc. (With extra letter at the end)
So, this special format doesn't work with #Jahidul Islam's answer. However, inspired by his answer, I manage to come up with this logic:
var code = "TQB2001M";
var i = course.indexOf(RegExp(r'[^A-Za-z]')); // get the index
var j = course.substring(0, i); // extract the first half
var k = course.substring(i).trim(); // extract the others
var formatted = '$j $k'.toUpperCase(); // combine & capitalize
print(formatted); // TQB 1011M
Works with other formats too. Check out the DartPad here.
Here is the entire logic you need (also works for multiple whitespaces!):
void main() {
String courseCode= "MMM 111";
String parsedCourseCode = "";
if (courseCode.contains(" ")) {
final ensureSingleWhitespace = RegExp(r"(?! )\s+| \s+");
parsedCourseCode = courseCode.split(ensureSingleWhitespace).join(" ");
} else {
final r1 = RegExp(r'[0-9]', caseSensitive: false);
final r2 = RegExp(r'[a-z]', caseSensitive: false);
final letters = courseCode.split(r1);
final numbers = courseCode.split(r2);
parsedCourseCode = "${letters[0].trim()} ${numbers.last}";
}
print(parsedCourseCode);
}
Play around with the input value (courseCode) to test it - also use dart pad if you want. You just have to add this logic to your input value, before submitting / handling the input form of your user :)

How can I manipulate a string in dart?

Currently I'm working in a project with flutter, but I realize there is a need in the management of the variables I'm using.
Basically I want to delete the last character of a string I'm concatenating, something like this:
string varString = 'My text'
And with the help of some method or function, the result I get:
'My tex'
Am I clear about it? I'm looking for some way which helps me to 'pop' the last character of a text (like pop function in javascript)
Is there something like that? I search in the Dart docs, but I didn't find anything about it.
Thank you in advance.
You can take a substring, like this:
string.substring(0, string.length - 1)
If you need the last character before popping, you can do this:
string[string.length - 1]
Strings in dart are immutable, so the only way to do the operation you are describing is by constructing a new instance of a string, as described above.
var str = 'My text';
var newStr = (str.split('')..removeLast()).join();
print(newStr);
Another way:
var newStr2 = str.replaceFirst(RegExp(r'.$') , '');
print(newStr2);

How to replace part of string with asterisk in Flutter?

I want to replace part of the string with asterisk (* sign).
How can I achieve that? Been searching around but I can't find a solution for it.
For example, I getting 0123456789 from backend, but I want to display it as ******6789 only.
Please advise.
Many thanks.
Try this:
void main(List<String> arguments) {
String test = "0123456789";
int numSpace = 6;
String result = test.replaceRange(0, numSpace, '*' * numSpace);
print("original: ${test} replaced: ${result}");
}
Notice in dart the multiply operator can be used against string, which basically just creates N version of the string. So in the example, we are padding the string 6 times with'*'.
Output:
original: 0123456789 replaced: ******6789
try using replaceRange. It works like magic, no need for regex. its replaces your range of values with a string of your choice.
//for example
prefixMomoNum = prefs.getString("0267268224");
prefixMomoNum = prefixMomoNum.replaceRange(3, 6, "****");
//Output 026****8224
You can easily achieve it with a RegExp that matches all characters but the last n char.
Example:
void main() {
String number = "123456789";
String secure = number.replaceAll(RegExp(r'.(?=.{4})'),'*'); // here n=4
print(secure);
}
Output: *****6789
Hope that helps!

How to delete a "(" from string in a list.toString()

I want to create a string of a list in a Text that don't add the parentheses "(string)" to de string
I tried to add this: .replaceAll(RegExp('('), "") but nothing
Text(
roupList.groupListName
.map((n) => '$n'.replaceAll(RegExp('('), ""))
.toString(),
overflow: TextOverflow.ellipsis,
),
what i get now is "(mystring,mystring,mystring)"
what i would like no parentheses "mystring,mystring,mystring"
Thanks
Is is easier than that:
String s = myList.map((listElement) => listElement.myStringProperty).join(",");
If you already have a list of Strings you could even do:
String s = myList.join(",");
String replace method does work. I don't know dart syntax but traverse in list with for,foreach etc and change it like below or you can use join.
String str1 = "Hello World";
str1=("New String: ${str1.replaceAll('World','ALL')}");
It was close but not what i wanted , finally found that the problem was this as ( is special character i need to add \
so i done this and it works
Text(
groupList.groupListName
.map((n) => '$n')
.toString()
.replaceAll(RegExp(r'\)'), '')
.replaceAll(RegExp(r'\('), ''),

error .match expression results null

I am working on a mail merge script. I have used Logger.log to find out that the error is in the expression that tells match what to find. In my case I am trying to pull all the keys that are inside ${xxxxxxx}. Below is what I have and I need help cleaning it up because at this point it returns null.
var template = "This is an example ${key1} that should pull ${key2} both keys from this text."
var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);
Thanks for any guidance anyone can share on this problem.
-Sean
I am not really familiarized with Google Apps Script, but I think this code in Javascript can help you.
It looks for all the ocurences of ${key} and returns each value inside the ${ }. I think that is what you are looking for.
var template = "This is an example ${key1} that should pull ${key2} both keys from this text.";
var matches = template.match(/\$\{[0-9a-zA-Z]*\}/mg);
console.log(matches);
for ( var i = 0; i < matches.length; i++ ) {
console.log(matches[i].replace(/[\$\{|\}]/gm, ""));
}