flutter: change strings letters from the letters from list 1 to the letters from list 2 - flutter

I am trying to change a string letters from the letters from list 1 to the letters from list 2
and I couldn't find a way to do it
this is my 2 lists
List En = ["A","A","B","G","D","R","S","C","T","E","F","K","L","M","N","H","W","Y","Y"];
List Ar = ["ا","أ","ب","ج","د","ر","س","ص","ط","ع","ف","ق","ل","م","ن","ه","و","ى","ي"];
so if the string was "abc" for example it would get the equivalent of the chars A B C from list 1 and then translate them to the same indexes in list 2

I don't know why others are happy with linear searches of lists. To me, that screams for setting up a map one time, and using it repeatedly. Here's what I whipped up in DartPad:
void main() {
var En = ['a', 'b', 'c'];
var Ar = ['1', '2', '3'];
var en2ar = Map<String, String>.fromIterables(En, Ar);
print(en2ar);
var text = 'abcd';
var output =
text.replaceAllMapped(RegExp('.'), (Match m) => en2ar[m.group(0)] ?? '');
print('$text => $output');
}

you can use this function:
setText(){
String input = 'ABC';
List text = input.split('');
String output = '';
List En = ["A","A","B","G","D","R","S","C","T","E","F","K","L","M","N","H","W","Y","Y"];
List Ar = ["ا","أ","ب","ج","د","ر","س","ص","ط","ع","ف","ق","ل","م","ن","ه","و","ى","ي"];
text.forEach((item){
int index = En.indexWhere((element) => element == item);
if(index != -1){
output = output + Ar[index];
}
});
return output;
}

using the indexWhere method you can find the index of a certain char in array a then replace the char with the element at the same index in array b
I would use this flow
cycle the characters of the string you want to repalce
for every character you get the index of the array a
add the item at the same index in the array b to a "result" string
You should now have a string "result" with the characters replaced

Related

Single user defined function that preprocesses a python list of strings

I have the following list of strings
my_list = ["This: is the first string", "This: is another String", This: is the third string of words in the list!"]
I want to create a function that takes each string from my_list in string format and removes the "This: " (the first 6 characters), punctuations, and stop words.
This is what I have tried:
def preprocess(any_list):
[e[6:] for e in any_list]
return any_list
no_punct = [char for char in any_list if char not in string.punctuation]
no_punct = ''.join(no_punct)
clean_words = [word for word in no_punct.split() if word.lower() not in stopwords('english')]
return clean_words
preprocess(my_list)

Filter strings containing a word in Flutter

I want to filter a list and remove Strings not containing words starting with a particular string.
Fe.: searching for words starting with "some"
"That is a list of some animals" - should be in the result
"That is a list of something like animals" - should be in the result
"That is a list of handsome animals" - should not be in the result
Might not be the most performant, but unless you're doing this on millions of items, there shouldn't be any problem:
final l = [
'That is a list of some animals',
'That is a list of something like animals',
'That is a list of handsome animals',
];
l.retainWhere((str) => str.split(' ').any((word) => word.startsWith('some')));
The question is already been answered in the simplest form of code. But I am doing it in a layman's way.
final list = [
'That is a list of some animals',
'That is a list of something like animals',
'That is a list of handsome animals',
];
for(var i = 0 ; i < list.length ; i++){
var sentence = list[i].split(' ');
bool found = false;
for(var j = 0 ; j < sentence.length ; j++){
if (sentence[j].startsWith('some')){
found = true;
}
}
if(!found){
list.removeAt(i);
found = false;
}
}
Stored three sentences in the list.
Used a loop to get each sentence.
Split the sentence on the basis of space, to get each word.
Used an array to check each word of the sentence, whether it is starting from some
If any word of the sentence is starting from some, then I removed that sentence from the list

Swift 5 split string at integer index

It used to be you could use substring to get a portion of a string. That has been deprecated in favor on string index. But I can't seem to make a string index out of integers.
var str = "hellooo"
let newindex = str.index(after: 3)
str = str[newindex...str.endIndex]
No matter what the string is, I want the second 3 characters. So and str would contain "loo". How can I do this?
Drop the first three characters and the get the remaining first three characters
let str = "helloo"
let secondThreeCharacters = String(str.dropFirst(3).prefix(3))
You might add some code to handle the case if there are less than 6 characters in the string

How to use split method with string containing brackets?

I have a string that contains some data. Data is separated like this:
var stringData = (SomeWordsWithSpacesInBetween) 0 (SomeWordsWithSpaceInBetween) 1 ...
I want to be able to extract data between the brackets and numbers between the words in brackets as such:
stringData.split( some way to split them)[0] = SomeWordsWithSpacesInBetween;
stringData.split(some way to split them)[1] = 0;
How to split them this way?
var s = '(Some Words With Spaces InBetween) 0 (SomeWordsWithSpaceInBetween) 1';
var r = RegExp(r'\(((\w+ ?)*)\) (\d+) ?').allMatches(s).expand((e) => [e[1], e[3]]);
You can do it using regular expression. Here is an example.
List<String>getStringList(){
String abc = '(SomeWordsWithSpacesInBetween) 0 (SomeWordsWithSpaceInBetween) 1 (SomeWordsWithSpaceInBetween)';
List<String> myList = new List();
RegExp exp = new RegExp(r"\) (\d+) \(");
myList = abc.split(exp);
print('${myList}');
return myList;
}

Split comma delimited string into smaller ones

How would I split a comma delimited string into smaller comma delimited strings?
My string looks like this: 1,2,3,4,5,6,7,8,9,10
And I need to split the string after every nth occurrence of the , character.
E.g. for every 3rd occurrence, the above string would be turned into these strings:
1,2,3,4 5,6,7,8 9,10
Might look like homework but it's not, my brain is just tired but I still need to get work done.
Try a loop in which you count the commas ;-)
Untested, it could look like:
int lastSplit = 0;
int commaCount = 0;
int n = 4;
List<string> parts = new List<string>();
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ',' && ++commaCount == n)
{
commaCount = 0;
parts.Add(s.Substring(lastSplit, i - lastSplit));
lastSplit = i + 1;
}
}
parts.Add(s.Substring(lastSplit));
You could do it via regex. Try out ((?:[^,]+)(?:,(?:(?:[^,]*))){0,3}) on rubular
Oh, and then you just need to swap out the "3" in the regex for whatever number of commas you need.
So?
[TestMethod]
public void test()
{
string text = "1,2,3,4,5,6,7,8,9,10";
var lists = Regex.Matches(text, ".,.,.,.");
foreach (var x in lists)
{
Console.WriteLine(x.ToString());
}
}