Is there any way to merge String item in list with another string ?
for example:
I have this list
final list1 = ['Hello'];
i need when click on button add World word to Hello word
so the list will be
['Hello World']
Use
final list1 = ['Hello']; // given
String hello = list1[0]; // get first letter
String world = 'World'; // letter you want to add
list1[0] = hello + ' ' + world; // concatenate both string and update list
Related
I'm currently extracting hashtags from a string and adding them to an array but how can I remove them from the string as well provided they are located by the end?
For example:
// Random string with tags
String message = "Hello #tag1 and #tag2 the end #tag3 #tag4";
RegExp regex = RegExp(r"\B#\w\w+");
List<String> tags = [];
// Add each tag to list
regex.allMatches(message).forEach((match){
tags.add(match.group(0)!);
});
How to remove #tag3 #tag4 from the original string so it's updated to look:
Hello #tag1 and #tag2 the end
The only reason I'm keeping #tag1 and #tag2 is because it would look weird if they were taken out since it's within a sentence. Note that the string can be anything.
Original code taken from here.
Try this:
String text = "Test #tag1 and #tag2 end #tag3 #tag4";
String resultText = "";
var l = text.split(" ");
int index = l.lastIndexWhere((element) => !element.contains('#'));
l.removeRange(index + 1, l.length);
resultText = l.join(' ');
print("resultText= $resultText"); //resultText= Test #tag1 and #tag2 end
for example if I have a string = "I Like To Play Football" and a list = [Car,Ball,Door,Sky] it should give true.
Use any of list
var list = ["Car","Ball","Door","Sky"];
String text = "i like to play football";
if (list.any((item) => text.toLowerCase().contains(item))) {
//Text has a value from list
}
Here You Go:
void main() {
final String tempString = "I Like To Play Football";
final List<String> tempList = ["Car","Ball","Door","Sky"];
for(var i=0; i < tempList.length; i ++) {
if(tempString.contains(tempList.elementAt(i).toLowerCase())){
print("Found and its ${tempList[i]}");
}
}
}
Regex is your friend here. You can make a simple regex that uses each string in the array as an option (and make it case insensitive) then run the match. I've made an example here in javascript, but it's easy to do in dart
https://api.dart.dev/stable/2.16.1/dart-core/RegExp-class.html
const source = "I Like To Play Football";
const toMatch = ["Car","Ball","Door","Sky"];
let regexString = '';
for (const option of toMatch) {
//adding | modifier after string. Last one is redundant of course
//also I'm not checking for special regex characters in toMatch, but that might be necessary.
regexString += option + '|';
}
// using slice to remove last |
console.log(regexString.slice(0, -1));
const regexp = new RegExp(regexString.slice(0, -1), 'i');
console.log(source.match(regexp));
Here's a short version:
var src = 'I Like To Play Football'.split(' ');
var list = ['Car','Ball','Door','Sky'];
var result = list.any((x) => src.any((y) => y.toLowerCase().contains(x.toLowerCase())));
print(result);
I want to extract from the full String this part:
Will
full String:
"Will posted an update in the group Testing"
Another example
longerName
full String:
"longerName posted an update in the group Testing"
any help please
This could work:
String str = "Will posted an update in the group Testing";
String result = str.split("</a>")[0] + "</a>";
From the top of my head, this could work.
Regex
<a\b[^>]*>(.*?)</a>
Dart
final myString = "Will bla bla bla";
final regexp = RegExp(r'<a\b[^>]*>(.*?)</a>');
// final match = regexp.firstMatch(myString);
// final link = match.group(0);
Iterable matches = regexp.allMatches(myString);
matches.forEach((match) {
print(myString.substring(match.start, match.end));
});
I'm calculating how many different words there are. How can I find the most used word in the text. How can I add this to the code.
int kacFarkliKelime(String metin) {
String yeniMetin = metin.replaceAll(RegExp(r'[^\w\s]+'), "");
List<String> liste = yeniMetin.split(
" ",
);
List farklilar = [];
liste.forEach((element) {
String sorgulanan = element.toLowerCase();
if (!farklilar.contains(sorgulanan)) {
farklilar.add(sorgulanan);
}
});
if(farklilar[0])
return farklilar.length;
}
I'd build a Map<String, int> that'd count each word as it is seen, then get a list of keys sorted by descending value order. (There's a few handy "sort by" functions in package:collection to help with that.) The code for that would look like:
var words = (use a regex to extract words);
var count = <String,int>{};
for (final w in words) {
count[w] = 1 + (count[w] ?? 0);
}
var ordered = count.keys.toList();
ordered.sort((a, b) => count[b].compareTo(count[a]));
Now the first element of ordered is the word with the most appearances in the text.
I have number of Strings coming from an API.
What I want is to merge all Strings together...
What I've done so far is store all Strings in an Array and convert that to a String:
var a = List<String>();
a.add("\n \u2022 " + "test1");
a.add("\n \u2022 " + "test2");
Result:
[•test1
•test2
]
Expected:
bulleted lists without [] .
Is there a better way to do this?
This code sample should answer your questions:
void main() {
const itemPrefix = " \u2022 ";
// create a growable list of strings
final strings = <String>[];
// add some items to it
strings.add("test1");
strings.add("test2");
// create a single string joining the items
String result = strings
// prepend the bullet point to each item
.map((item) => "${itemPrefix}$item")
// put a new-line between each item, joining the items to a String
.join('\n');
print(result);
}