Split
No edit summary |
(Automatically adding template at the end of the page.) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
In OCL, the | In OCL, the String split function gets expects a char - but OCL defaults single characters to [[String]]. To solve this, do this: | ||
SomeString.split('X'.toCharArray) | SomeString.split('X'.toCharArray) | ||
This OCL SomeString.split('X') will give you the error: ''31:System.String does not conform to Collection(System.Char)'' | This OCL SomeString.split('X') will give you the error: ''31:System.String does not conform to Collection(System.Char)'' | ||
An example of creating a | An example of creating a <code>Collection(Part1:System.String+Part2:System.String)</code> which is a [[tuple]] that you can iterate to - for example, do search and replace: | ||
'A, B; | 'A, B; | ||
X, Y'.Split(';'.toCharArray)->collect(pair | pair.Split(','.toCharArray)->at(1), pair.Split(','.toCharArray)->at(2)) | X, Y'.Split(';'.toCharArray)->collect(pair | pair.Split(','.toCharArray)->at(1), pair.Split(','.toCharArray)->at(2)) | ||
Line 12: | Line 12: | ||
If you want to do the reverse, look at [[OCLOperators asSeparatedList|asSeparatedList]]. | If you want to do the reverse, look at [[OCLOperators asSeparatedList|asSeparatedList]]. | ||
[[Category:OCL]] | [[Category:OCL]] | ||
{{Edited|July|12|2024}} |
Latest revision as of 15:47, 10 February 2024
In OCL, the String split function gets expects a char - but OCL defaults single characters to String. To solve this, do this:
SomeString.split('X'.toCharArray)
This OCL SomeString.split('X') will give you the error: 31:System.String does not conform to Collection(System.Char)
An example of creating a Collection(Part1:System.String+Part2:System.String)
which is a tuple that you can iterate to - for example, do search and replace:
'A, B; X, Y'.Split(';'.toCharArray)->collect(pair | pair.Split(','.toCharArray)->at(1), pair.Split(','.toCharArray)->at(2))
RGB := '241, 55, 45'; '#' + RGB.split(','.toCharArray)-> collect(s|Integer.parse(s))-> collect(i|i.toString('x'))->asSeparatedList('')
If you want to do the reverse, look at asSeparatedList.
This page was edited more than 11 months ago on 02/10/2024. What links here