substring of a variable
In a rule I want to take apart DeviceCustomSring1 which has the value "#999.999" into two fields. So I need to:
(1) Find x = index_of("#999.999", ".")
(2) Find y = SubString( "#999.999", 1, x)
So can I save X above as an intermediate value in this rule and then use x as input to y?----regards, WER
You acutally have it already. Just store the result of the first funtion into a variable and use that variable in the next function.
getDotIndex = indexOf(deviceCustomString1,".")
getSubString1 = substring(deviceCustomString1,0,getDotIndex)
getAdvIndex = add(getDotIndex,1)
getSubString2 = substring(deviceCustomString1,getAdvIndex,-1)
The getSubString1 will give you the first part, and the getSubString2 will get you the second part. The getAdvIndex is needed to advance the index one, unless you want the dot.
I agree with chris. There is also another way to get the second substring.
1. Use a lengthof variable to grab the total number of characters in the string
2. use a subtract variable to subtract the integer from your first indexof from the integer from lengthof variable
3. substring the string starting your cut at 'subtractvariable' and stopping at 'lengthofvariable'.
this should work best for grabbing the second substring
I had to solve the same issue today...
I have a string, seperated by pipe symbol.
I created two helper variables, containing the position of the seperators:
But if you use those both to get the substrings into your variables, your delimiter will be part of it as the substring bondaries include the postions of the seperator.
So you have to
- subtract the sep_01 by one to get the "1111" into substring_01 instead of "1111|"
Therefore i created another helper variable, called sep_01-minus by using sep_01-minus=subtract(sep_01;1)
Now i can get the substring_01 variable filled by
this goes the same way for substring_02 which is substring(deviceCustomString;sep_01-plus;sep02-minus)
and for substring_03 as substring(deviceCustomString;sep_02-plus;-1)
Feels not like the silver bullet or very intuitive but IMHO this is the only way as you cannot combine functions within a single variable definition.
Furthermore you probably don't want the seperator character being a part of your custom variable.
I might have misunderstood, but if you want '1111' for substring_01, you need to use sep_01, not sep_01-minus. The substring function does NOT include the character at the position specified by the second parameter.
By the way, there's another way to manipulate strings: velocity template. It's way more powerful and convenient. The catch, however, is you can't use variables with velocity code for reports.
There was one time when I had to write 60 variables to process a string. The samething took 2 lines of velocity code.