For instance, this list contains the following strings:
array[0]: "text1,text2,text3,text4,text5"
array[1]: "text1,text2,text3,text4,text5,text6,text7"
array[2]: "text1,text2,text3,text4,text5,text6"
I need to ignore the first 3 texts and take the rest.
Output should be:
"text4,text5"
"text4,text5,text6,text7"
"text4,text5,text6"
I suppose I need to split the string using the comma and iterate from 2 till array length for each element.
Is there an elegant way to perform this?
Hi you can use list comprehension
array = ["text1,text2,text3,text4,text5", "text1,text2,text3,text4,text5,text6,text7", "text1,text2,text3,text4,text5,text6"]
result = [i.split(",")[2:] for i in array]
print(result)
For Groovy, you can do something like this:
def input = [
"text1,text2,text3,text4,text5",
"text1,text2,text3,text4,text5,text6,text7",
"text1,text2,text3,text4,text5,text6",
]
def result = input.collect {
it.split(',').drop(3).join(',')
}
result.each { println it }
which prints out
text4,text5
text4,text5,text6,text7
text4,text5,text6
For lovers of 1-liners:
def input = [
"text1,text2,text3,text4,text5",
"text1,text2,text3,text4,text5,text6,text7",
"text1,text2,text3,text4,text5,text6",
]
def output = input*.split(',')*.getAt( 3..-1 )*.join(',')
output.each this.&println
prints:
text4,text5
text4,text5,text6,text7
text4,text5,text6
array = array.collect{ i-> i[3..-1] }
if you want just to print
array.each{ i-> println i[3..-1].join(',') }
I am parsing each element of a list one by one.
def List1 = (String[]) Data[2].split(',')
Part of this list gives me a list with elements that contain a delimiter !.
List1 = [TEST1!01.01.01, TEST2!02.02.02]
I tried to iterate each element of this list and obtain a comma separated list.
def List2 = []
List1.each { List2.add(it.split('!'))}
However, the result was a list of list.
[[TEST1, 01.01.01], [TEST2, 02.02.02]]
Instead of [TEST1, 01.01.01, TEST2, 02.02.02].
How do I avoid this and obtain a list as shown above?
How about this?
def list1 = ['TEST1!01.01.01', 'TEST2!02.02.02']
println list1.collect{it.split('!')}.flatten()
When you do List2.add(it.split('!')), you are adding list to List2 instead of single string because .split() creates a list from string.
You should firstly create list by using .split() and than add each member of list to List2.
Here is solution:
def List1 = ["TEST1!01.01.01", "TEST2!02.02.02"]
def List2 = []
List1.each { List1member ->
def subList = List1member.split('!')
subList.each { subListMember ->
List2.add(subListMember)
}
}
println(List2)
split() returns a list. That is the reason why I got a list of list. I found that split() can carry process multiple delimiters as well when applied with an operator.
The following returns the desired output.
def List1 = (String[]) Data[2].split(',|!')
i got a list like this, However my method isn't working, any help would be appreciated. Thanks
//My list of values
List<string> values = new List { "$100","$300","$500"};
In my button1_click method
int i = 2;
object o = values[i];//i want the i to have value 2
this.button1.Text = Convert.ToString(i); //i want the button to text out the value from the values[i].
You're converting the wrong item.
this.button1.Text = Convert.ToString(i); //<---- i should be o
Change to:
this.button1.Text = Convert.ToString(o);
But you really shouldn't be converting from string to object and back to string.
Could simply just use:
this.button1.Text = values[i];
You have a list of strings, therefore there's no need to convert its value.
this.button1.Text = values[i];
The following piece of Groovy code prints an empty list:
List<String> list = ["test-${1+2}", "test-${2+3}", "test-${3+4}"]
List<String> subl = ["test-1", "test-2", "test-3"]
println subl.findAll { it in list }
Output:
[]
However, this modification leads to the correct output:
List<String> list = ["test-${1+2}" as String, "test-${2+3}" as String, "test-${3+4}" as String]
List<String> subl = ["test-1", "test-2", "test-3"]
println subl.findAll { it in list }
Output:
[test-3]
But this "solution" feels very clunky.
What is the correct Groovy way to achieve this?
You can use the *. spread operator to get Strings easily (see list2 example below). But your check there can be done alot easier with intersect.
List<String> list = ["test-${1+2}", "test-${2+3}", "test-${3+4}"]
List<String> subl = ["test-1", "test-2", "test-3"]
assert subl.findAll{ it in list }==[] // wrong
// use intersect for a shorter version, which uses equals
assert subl.intersect(list)==['test-3']
// or with sets...
assert subl.toSet().intersect(list.toSet())==['test-3'].toSet()
// spread to `toString()` on your search
List<String> list2 = ["test-${1+2}", "test-${2+3}", "test-${3+4}"]*.toString()
assert subl.findAll{ it in list2 }==['test-3']
I have a list defined as below inside a method
List<string> testList = new List<string>() {"A","A","B","A","A","A","B"};
Here my condition will be fixed say where the element matches "A". Based on upon my input, for instance 2, logic should identify two consecutive A's in the list and return them. In the above list it should return first and second elements. If my input is 3 it should return fourth,fifth and sixth elements where three A's are consecutive. If it is 4 it should not return anything. Is there a simple way of implementing this in C#
This is one way to do what you want:
List<string> testList = new List<string>() {"A","A","B","A","A","A","B"};
string inputText = <your input text>;
int inputCount = <your input count>;
var zipped = testList.Zip(Enumerable.Range(0,testList.Count-1), (txt,idx) => new {txt,idx});
var result = zipped
.Where(combined => combined.idx <= testList.Count-inputCount)
.Where(combined => combined.txt == inputText
&& testList.GetRange(combined.idx, inputCount).All(z => z == inputText));
We zip with the ordered range [0..list count - 1] to provide indexes for your elements. Then we check if current element in the collection equals the input text, and if the first n elements starting from current one all equal input text, where n is the input count.
We can then use a foreach loop to iterate through result like so:
foreach(var r in result)
{
testList.GetRange(r.idx,inputCount).ForEach(x => Console.WriteLine(x));
}
For each pattern that is found, we use GetRange to get the corresponding values.
Note that we are creating another IEnumerable object when we call Zip, and also calling GetRange within the Where clause, so I don't think this method is very efficient. Nevertheless, it will solve the issue.