0

I found a good solution but not exactly for my problem:

String s = "abc.def.ghfj.qert"; 
s.substring(s.lastIndexOf(".") + 1)

Source: Java: Getting a substring from a string starting after a particular character

This I have to use it within an array, but I have NO idea how to do... sorry.

I got the following code:

a = [];
b=getLinkedItems(item("link_M"))
for(i in 0..b.size()-1)
{ a[i] = b.getAt(i).toString().reverse().substring(0,1) }
total = 0
for(x in a) { total = total +","+x }
val = total.substring(2)

This is my result:

[b] = [java.util.ArrayList] [

Group:m.1:book1150:MGROUP.6/M/m.1, 

Group:m.6:book1150:MGROUP.6/M/m.6, 

Group:m.9:book1150:MGROUP.6/M/m.9]

[a] = [java.util.ArrayList] [1, 6, 9]

[total] = "0,1,6,9"

[val] = "1,6,9"

... but it is not practicable if my array contains a value greater than 9.

What can I do if my array is e.g.: 

[b] = [java.util.ArrayList] [

Group:m.1:book1150:MGROUP.6/M/m.1, 

Group:m.6:book1150:MGROUP.6/M/m.6, 

Group:m.9:book1150:MGROUP.6/M/m.12]

and my result should be:

[val] = "1,6,12"

Who can help me? Where do I have to write e.g. "lastIndexOf(".")" or do anyone have a better solution?

Thank you in advance.

Community
  • 1
  • 1

2 Answers2

1

Your syntax in the question is confusing, but assuming you mean you have:

def list = [
    'Group:m.1:book1150:MGROUP.6/M/m.1', 
    'Group:m.6:book1150:MGROUP.6/M/m.6', 
    'Group:m.9:book1150:MGROUP.6/M/m.12'
]

then you can just do:

def val = list.collect { row -> row.findAll(/\d+/)[-1] }.join(',')

For the bintan Group stuff, try:

def val = list.collect { row -> row.toString().findAll(/\d+/)[-1] }.join(',')
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • My syntax was correct copied. When I try your code I get this response: Error: No signature of method: bintan.form.Group.findAll() is applicable for argument types: (java.lang.String) values: [\d+] – PegasusTT77 Sep 21 '16 at 15:42
  • WTH is `bintan.form.Group`? – tim_yates Sep 21 '16 at 15:46
  • I would guess that `bintan.form.Group` is the actual type returned by his `getLinkedItems` method, since that is the only place a `findAll` is used in your code. – BalRog Sep 21 '16 at 15:49
  • @PegasusTT77 added a `toString()` call to the collect – tim_yates Sep 21 '16 at 15:51
  • How are you handling strings that end in multi-digit numbers? `[-1]` will only grab the last digit. – BalRog Sep 21 '16 at 15:53
  • It will grab the last result from `findAll`, which is looking for `\d+` – tim_yates Sep 21 '16 at 15:53
  • Yes, but you are only taking the last character, (index `[-1]`), so it doesn't matter what the findAll expression matches. if the `String` value of his element were `"Group:m.9:book1150:MGROUP.6/M/m.12"`, you would only return `2`, not `12`. – BalRog Sep 21 '16 at 16:04
  • @tim_yates: My system works based on Groovy language (eclipse). I am sorry that I did not write it within my question. – PegasusTT77 Sep 21 '16 at 16:05
  • 1
    @BalRog no it doesn't...have you tried it? `assert "Group:m.9:book1150:MGROUP.6/M/m.12".findAll(/\d+/)[-1] == '12'` – tim_yates Sep 21 '16 at 16:08
  • @tim_yates: Now it worked with your correction of "bintan Group stuff". :-)Sorry, I told I am a new user. Shame on me. – PegasusTT77 Sep 21 '16 at 16:15
  • @PegasusTT77 No worries :-) I missed the `toString` in your question... Glad you got it sorted :-) – tim_yates Sep 21 '16 at 16:23
0

I would recommend using regular expressions instead, and maybe some List-based mix-ins for good measure. Something like this maybe:

def b = getLinkedItems(item("link_M"))
def a = b.collect { (it.toString() =~ /\d+$/)[0] }
def val = a.join(",")
def total = "0," + val

In this, b.collect runs the Closure that follows once for each element of b, returning a List of results to assign to a. The find (=~) operator returns a Matcher object that finds all instances of the regular expression on the right (/\d+$/ in this case) within the source String on the left. The matched values can be retrieved from the Matcher by indexing it like a List. Then on the next line, a.join(",") joins together all the elements of a with , as a separator.

The regular expression /\d+$/ means:

  • \d+ any substring of one or more digits
  • $ at the end of the source String
BalRog
  • 3,216
  • 23
  • 30