Velocity template substring issue - java

I have an issue with extracting a substring in velocity.
the string I have is 1M/1Y (the variable string here)
I need to extract 1M and 1Y.
what is the best way to do it?
#set($index=$string.index('/'))
#set($val=$string.substring($index,index+2))
what am I doing wrong here?

In velocity template we have access to all the public methods of the String class.
Try using the below code
#set ($index = $string.indexOf('/'))
#set ($val1= $string.substring(0, $index))
#set ($index = $index + 1)
#set ($val2 = $string.substring($index))
or you can also make use of $string.split("/") if you are using Velocity 1.7

You can use stringUtil:
#set($parts = $stringUtil.split($string, "/"))
$parts.get(1)
$parts.get(2)
....

You missed $ before the last 'index' variable, this should fix your code:
#set($index=$string.index('/'))
#set($val=$string.substring($index,$index+2))

Related

Selenium via java - sendKeys doesn't send specific chars to input

I'm having a strange condition where i'm trying to type into input by using sendKeys , the reuslt is that specific chars doesn't seem to be implemented in the input at all.
What i'm trying to do:
webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)")).sendKeys("(test)");
the result is that input field is now : test) and the missing char is '(' .
If i will try
webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)")).sendKeys("((((((((((")
the result is that the input is empty.
Anyone ever faced this issue before? it is happening on a very specific input in the app, couldn't find anything related to it in the html code.
Thanks in advance.
Edit: I can manually type ( in the input field.
Maybe it's a special character for selenium, have you tried using escape characters? Something like backslash before it if it allows it.
Edit: I found some issue report on github from last year, not sure if they agreed to not fix it. Executing a script to type "(" seems to be an alternative.
Source: https://github.com/seleniumhq/selenium/issues/674
try declaring the key as a string first
String keyToSend = "(test)";
webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)")).sendKeys(keyToSend);
In this case you should try using JavascriptExecutor as below :-
WebElement el = webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)"));
((JavascriptExecutor)webDriver).executeScript("arguments[0].value = arguments[1]", el, "(test)");
Hope it helps..:)

Using Velocity combined with Java, I'm trying to pull a single field from a '.' delimited string

I have my velocity directive set as #set ($stringList = $string.split("."))
I have tried the following in my syntax but can't get it to work
$stringList.get(0)
$stringList[0]
$stringList.[0]
${stringList}.get(0)
change
#set ($stringList = $string.split("."))
to
#set ($stringList = $string.split("\\."))
and access it like
$stringList[0]

Replace design pattern in query string

I have currently some URL like this :
?param=value&offset=19&size=100
Or like that :
?offset=45&size=50&param=lol
And I would like to remove for each case the "offset" and the "value". I'm using the regex method but I don't understand how it's really working... Can you please help me for that?
I also want to get both values of the offset and the size.
Here is my work...
\(?|&)offset=([0-9])[*]&size=([0-9])[*]\
But it doesn't works at all!
Thanks.
Assuming is Javascript & you only want to remove offset param:
str.replace(\offset=[0-9]*&?\,"")
For Java:
str=str.replaceAll("offset=[0-9]*&?","");
//to remove & and ? at the end in some cases
if (str.endsWith("?") || str.endsWith("&"))
str=str.substring(0,str.length()-1);
With out regex .
String queryString ="param=value&offset=19&size=100";
String[] splitters = queryString.split("&");
for (String str : splitters) {
if (str.startsWith("offset")) {
String offset = str.substring(str.indexOf('=') + 1);//like wise size also
System.out.println(offset); //prints 19
}
}
If you need to use a regular expression for this then try this string in java for the regular expression (replace with nothing):
"(?>(?<=\\?)|&)(?>value|offset)=.*?(?>(?=&)|$)"
It will remove any parameter in your URL that has the name 'offset' or 'value'. It will also conserve any required parameter tokens for other parameters in the URL.

How to use split() in velocity template?

I am trying to split a string in velocity context to get an array in return like following--
#if($stringValue.split("::")[1].length()==0)
//some code
But it does not work for velocity.I am getting a parser error which is unable to compile []
So,how can I implement this logic in velocity???
Using velocity 1.7 and possibly below this can be done using the String split() method.
Unlike it's Java counterpart for special characters one doesn't need to escape the forward slash (.e.g "\\|").
#set ($myString = “This|is|my|dummy|text”)
#set ($myArray = $myString.split("\|")) or
#set ($myArray = $myString.split('\|')) or
#set ($myArray = $myString.split("[|]"))
Note 1: To get the size of the array use: $myArray.size()
Note 2: To get actual values use $myArray.get(0) or $myArray[0] … etc
Suggestion: one could use beforehand #if ($myString.indexOf(‘|’)) ... #end

Java string inside string to string

I have this string: "\"Blah \'Blah\' Blah\"". There is another string inside it. How do I convert that into: Blah 'Blah' Blah? (you see, unescaping the string.) This is because I get a SQL Where query:
WHERE blah="Blah \'Blah\' Blah"
When I parse this, I get the string above (still inside quotes and escaped.) How would I extract that, un-escaping the string? Or is ther some much easier way to do this? Thanks,
Isaac
DO NOT DO THIS.
Follow the proper steps for parametrization of a query on your Database/Platform, and you won't have to escape anything. You also will protect yourself from injection vulnerabilities.
Put the string in a property file, Java supports XML property files and the quote character does not need to be escaped in XML.
Use loadFromXML(InputStream in) method of the Properties class.
You can then use the MessageFormat class to interpolate values into the String if needed.
This should be about right. This assumes that if it starts with a quote, it ends with a quote.
if (val.startsWith("\"") || val.startsWith("\'"))
val = val.substring(1, val.length-2);
You may wish to add val = val.trim(); as well.
"\"Blah \'Blah\' Blah\"".replaceAll("\"", "")

Categories

Resources