Replace the last character or number of a string | Freemarker - freemarker

I need to check if the last number of character of a ${string} is equal to 9.
The string or numbers that I have to handle with is something 831, 519 or 1351.
However I dont know how do do it properly. I tried already something like:
${string?replace((string.length)-1,"9")}
At the end there should be instead of 831 --> 839 or 1351 --> 1359 and so on.
Any sugestions about how I can archive this ?
Oh and by the way. If I use the fuction above this error massage comes up:
Script error: You have used ?number on a string-value which is not a number (or is empty or contains spaces).
And what I tried also was:
code snippet
Because the original number is somethink like 831.896.

You could use string slicing to keep all characters except for the last one like this:
<#assign string = "1234">
<#assign string = string[0..<string?length-1] + "9">
${string}
Results in:
1239

Since you want to replace that thing, use ?replace. This replaces the last character with 9, if the last character is a digit that's not already 9:
${s?replace("[0-8]$", '9', 'r')}

Related

Ruby/Rspec assert that first 4 characters match given text

I have a timestamp
timestamp = 1466754627
And I want to assert that the first 4 characters are 1466 within a test. If i was to use
timestamp.includes('1466')
This will pass, but 1466 could appear anywhere and not necessarily the first 4 characters. How could i ensure that 1466 are the first 4 characters ?
Thanks
There's a start_with matcher:
expect('1466754627').to start_with('1466')
You might have to convert timestamp to a string via to_s if it is indeed an integer:
timestamp = 1466754627
expect(timestamp.to_s).to start_with('1466')

Ruby rspec check string begins with certain values

I have a string that begins with a set start and then is filled to 252 characters by randomly generated letters. (237 random chars - so that the end length is 252 characters)
For the purpose of testing the string starts off with TESTDATAMENDNOW and the rest is random capital letters.
How can I test that the characters are all capital letters
and that the string begins with TESTDATAMENDNOW
I have tried to use regex expressions to define this but I'm not too sure how to get them to work properly, and what I have tried so far seems not to be working.
EDIT: Clarity
expect(string).to match(/\ATESTDATAMENDNOW[A-Z]{237}\z/)
237 because 252 minus the length of "TESTDATAMENDNOW" is 237.
Here's another way that does not use a regex:
str = "TESTDATAMENDNOW"
expect(string[0,str.size]).eq(str)
expect(string.delete("ABCDEFGHIJKLMNOPQRSTUVWXYZ").eq("")
expect(string.size).eq(252)

Regex that doesn't accept spaces in between numbers

I am trying to parse lines like this:
0: abc 0.5
1: a 16.1,3
2: b 0.9,2.3
3: c -19.645
7:
which are in the format:
Number:[space][up to 4 letters from the range ABCD][space][comma separated numbers that could be decimal and/or negative]
with the ruby command below
if line=~ /^(\d*): [abcd]{0,4} ((\-?)(\d*).(\d*))*/) then
do x
else
do y
However, it also matches the strings below, which I don't want it to, since they have " " or ":" in between numbers instead of ",".
4: d 0.8 16.56
5: d 0.9:5.0
How can I modify my regex to make it work for only comma separators?
Edit: The Rubular link if you would like to edit the Regular Expression is as follows: http://rubular.com/r/8Z9Eeu27i5
If I understand correctly, this one should work:
^\d+:\s[a-zA-Z]+\s(-?(\d+\.)?\d+,)*(-?(\d+\.)?\d+)$
EDIT:
If 7:[space][space] is valid too, then use this one:
^\d+:\s[a-zA-Z]*\s(-?(\d+\.)?\d+,)*(-?(\d+\.)?\d+)?$

Separate word Regex Ruby

I have a bunch of input files in a loop and I am extracting tag from them. However, I want to separate some of the words. The incoming strings are in the form cs### where ### => is any number from 0-9. I want the result to be cs ###. The closest answer I found was this, Regex to separate Numeric from Alpha . But I cannot get this to work, as the string is being predefined (Static) and mine changes.
Found answer:
Nevermind, I found the answer the following sperates alpha-numeric characters and removes any unwanted non-alphanumeric characters so anything like ab5#6$% =>ab 56
gsub(/(?<=[0-9])(?=[a-z])|(?<=[a-z])(?=[0-9])/i, ' ').gsub(/[^0-9a-z ]/i, ' ')
If your string is something like
str = "cs3232
cs23
cs423"
Then you can do something like
str.scan(/((cs)(\d{1,10}))/m).collect{|e| e.shift; e }
# [["cs", "3232"], ["cs", "23"], ["cs", "423"]]

ASP Left function issue

I have this code and something odd happening when I'm running it.
I have field number like 101512 up to 101520. I've used LEFT function to get rid of last two digits and keep the 1015. When i runn loop function for the first one it gives me 1015 but for the rest it gives me 101 an it elminates the last digit like this:
d = Split(Request("field"),",")
For i = 1 To UBound(d)
Responce.Write(Left(d(i),4))
Next
Results
1015
101
101
101
...
Does anybody have any idea what is going on?
My guess is that Request("field") may be returning a string like the following:
101520, 101521, 101522
Note the space after each comma. Thus when you apply Left() and print the value to your HTML output you don't notice the space but you only see three digits as the space counted as the first digit
One thing to try to see if this is the case is to change the code to the following:
Left(Trim(d(i)), 4)
That way any spaces around the value are removed before Left() is applied.
Correct way to iterate over "multi value" request item is actually:
For i = 0 To Request("field").Count-1
Response.Write(Request("field").Item(i) & "<br />")
Next
This will iterate the actual values without using split at all..

Resources