How to use split method with string containing brackets? - flutter

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;
}

Related

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

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

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 split scala string with regular expression

I come up a pattern like
val pattern = "(\\w+)\\|(.*)\\|\\[(.*)\\]\\|\"(.*)\"\\|\"(.*)\"\\|\\[(.*)\\]\\|\\[(.*)\\]\\|(.*)\\|\\[(.*)\\]\\|\\[(.*)\\]".r
and I have a original string
var str = """AuthLogout|vmlxapp21a|[13/Jan/2016:16:33:15 +0100]|"66.77.444.44 uid=XXXXX,ou=People,o=Bank,o=External,dc=xxxx,dc=com"|"abcd_123_portalweb_w "|[]|[41]||[]|[]"""
then apply pattern to the string, but it is always empty.
val items = pattern.findAllIn(str).toList
If I understand what you're trying to do, perhaps using a giant regex isn't the easiest way: You can split by | and get rid of the unwanted separators ([, ], ") using replaceAll:
val str = """AuthLogout|vmlxapp21a|[13/Jan/2016:16:33:15 +0100]|"66.77.444.44 uid=XXXXX,ou=People,o=Bank,o=External,dc=xxxx,dc=com"|"abcd_123_portalweb_w "|[]|[41]||[]|[]"""
val withoutBoundaries = str.replaceAll("[\"\\]\\[]","")
val result = withoutBoundaries.split("\\|")
result.foreach(println)
Which prints:
AuthLogout
vmlxapp21a
13/Jan/2016:16:33:15 +0100
66.77.444.44 uid=XXXXX,ou=People,o=Bank,o=External,dc=xxxx,dc=com
abcd_123_portalweb_w
41
If you do want to use a regex here, I'd create sub-regex vars representing the different text parts that you're after, to make this somewhat manageable:
val plain = "(.*)" // no boundary characters
val boxed = s"\\[$plain\\]" // same, encapsulated by square brackets
val quoted = '"' + plain + '"' // same, encapsulated by double quotes
// the whole thing, separated by pipes:
val r = s"$plain\\|$plain\\|$boxed\\|$quoted\\|$quoted\\|$boxed\\|$boxed\\|$plain\\|$boxed\\|$boxed".r
val result = r.findAllIn(str).toList // this list has one item, as expected.
Now, if you want to see how this regex looks like, here it is - but I don't recommend having this in your code...:
val r = """(.*)\|(.*)\|\[(.*)\]\|"(.*)"\|"(.*)"\|\[(.*)\]\|\[(.*)\]\|(.*)\|\[(.*)\]\|\[(.*)\]""".r

How to split Matlab string into two with known suffix?

I need to split a string into two components. As an example I have the string:
s = 'Hello1_1000_10_1_data'
and I want to split it into the two strings
str1 = 'Hello1_1000_10_1'
and
str2 = '_data'
the important point is that I can't be too sure of the format of the first string, the only thing that is sure is that the 'suffix' which is to be read into the second string always reads '_data'. What is the best way to do this? I looked up the documentation on strtok and regexp but they do not seem to offer me what I want.
If you always know the length of the suffix, you could just use that:
s = 'Hello1_1000_10_1_data'
str1 = s(1:end-5)
Or otherwise:
s = 'Hello1_1000_10_1_data'
suffix = length('_data')
str1 = s(1:end-suffix)
You can use:
s = 'Hello1_1000_10_1_data';
str = regexp(s, '(.*)(_data)', 'tokens'){1};
str{1} %// Hello1_1000_10_1
str{2} %// _data
If _data occurs several times in the file name, this will still work.
You can also use strsplit() as follow:
s = 'Hello1_1000_10_1_data';
suffix = '_data';
str = strsplit(s,suffix);
str1 = str{1}
In addition, you can use strsplit() with multiple delimiters.
You can use strfind():
s = 'Hello1_1000_10_1_data';
suffix = '_data';
i = strfind(s,suffix);
if any(i)
i = i(end);
prefix = s(1:i-1);
end

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());
}
}