Velocity Variable(use together string and integer variables) - velocity

How can I use String and Integer variables it together in "Velocity"?
#set ($var1 = "exp"+1)
I tried a few times but I could not.
Any help will be highly appreciated.

If you were trying to do concatenation, it's not supported by Velocity, not even for 2 Strings. Instead, you should use string interpolation. Example:
#set ($string = "exp")
#set ($number = 1)
#set ($interpolatedExample1 = "$string$number")
#set ($interpolatedExample2 = "${string}someStringLiteral$number")
Read more details and examples in the documentation.

Related

how to evaluate ( math ) a string expression, vb.net

I'm not sure the term I used is correct. but I have a string expression and I want it to be calculated.
this an example
Dim S = "4+4"
dim result = evaluate(S) 'some sort of treatment // that return 8
I'm not sure how this is going to work. I'm familiar with the JS eval function. but it seems that I need to add some sort of a library. and I don't want to do that.
I have found some links about using
dim s = new expression("4+4")
and getting the result with
s.evaluate()
but that require to add another library.
and as I said before I don't want to use any library.
I just want a solution on how to proceed? I have hit a wall.
BTW I'm still a beginner try to answer as simple as you can I would appreciate it.
You can use the DataTable.Compute-"trick":
Dim tbl = new DataTable()
Dim result = Convert.ToDouble(tbl.Compute("4+4", Nothing))
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations: DataColumn.Expression at Expression Syntax.

How can I omit the first character for value in vb.net?

I have this var:
Dim number as decimal = -61.52
and I want to delete the first character to be like this:
61.52
I tried to do this. but doesn´t work:
number = Trim(Left(number , Len(number ) - 1))
How can I do this?
You're looking for Math.Abs(), which forces a number to not be negative.
If you really want to treat a number as a string and then do string manipulations on it, you want to use the ToString method on your number. This will let you specify how you want the number formatted.
Dim NumberAsString As String
NumberAsString = number.ToString()
NumberAsString = NumberAsString.SubString(1, NumberAsString.Length - 1)
Note: you shouldn't rely on the default implementation as it will use the current UI culture and not guaranteed to give consistent results if executed under different cultures than when you developed it.

Java - Index a String (Substring)

I have this string:
201057&channelTitle=null_JS
I want to be able to cut out the '201057' and make it a new variable. But I don't always know how long the digits will be, so can I somehow use the '&' as a reference?\
myDigits substring(0, position of &)?
Thanks
Sure, you can split the string along the &.
String s = "201057&channelTitle=null_JS";
String[] parts = s.split("&");
String newVar = parts[0];
The expected result here is
parts[0] = "201057";
parts[1] = "channelTitle=null_JS";
In production code you chould check of course the length of the parts array, in case no "&" was present.
Several programming languages also support the useful inverse operation
String s2 = parts.join("&"); // should have same value like s
Alas this one is not part of the Java standard libs, but e.g. Apache Commons Lang features it.
Always read the API first. There is an indexOf method in String that will return you the first index of the character/String you gave it.
You can use myDigits.substring(0, myDigits.indexOf('&');
However, if you want to get all of the arguments in the query separately, then you should use mvw's answer.

Output part of a string in velocity

apologies if I waffle or talk a bit of jibberish but I'm new to velocity, and these forums!
I need to check the contents of a string for a certain character and output the second part of the text if it appears. For example:
set ($string = "This is a long string *** but I only want to output this on my email").
I want to output all text after the 3 Asterisks. I've scoured the forums but cant quite find anything that helps me completely.
Velocity is just a façade for real Java objects, so you have access to all the public methods of the String class, including indexOf and substring. So try something like:
#set ($string = "This is a long string *** but I only want to output this on my email")
#set ($index = $string.indexOf('***'))
#set ($index = $index + 3)
#set ($end = $string.substring($index))
If you have more control over what objects you put in the context, you could add an instance of StringUtils as a helper tool, and then use substringAfter directly.

RegEx to get path of file, without domain

I'm new to regular expressions, and have no clue where to start, it's like a diff language to me. But I need one quick to accomplish a task.
I need to take
http://www.domain.com/folder1/folder2/file_path.txt
and get just
/folder1/folder2/file_path.txt
from it.
Thanks!
construct a URI object from it and one of the properties of it will have what you want.
I think that regex should work:
^http://.*?/(.*)$
(tested with Python)
Since VB.NET is in the tag for this question, I assume you have access at the server side to the Request object:
Dim instance As HttpRequest
Dim value As String
value = instance.Path
This should give you exactly what you asked for.
Edit: On second thought - you could be parsing URLs from some input string... in which case, regex will only help if you have a simple (regular) set of inputs:
Do you know all the possible domains? i.e. are "http://www.ABC.com" and "http://www.DEF.com" the only possible domains?
Then here:
Dim text As String = "http://www.ABC.com/folder1/folder2/file.txt"
Dim pattern As String = "(?:http://www.ABC.com|http://www.DEF.com)(.*)"
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim g as Group = m.Groups(2) 'Gives the string matched by the capturing parentheses
Supporting more protocols and making the protocol optional too.
((https?|ftp)://)?(.*?)/(.*)