How to replace string in Groovy - groovy

I have some string like
C:\dev\deploy_test.log
I want by means of Groovy to convert string to
C:/dev/deploy_test.log
I try to perform it with command
Change_1 = Log_file_1.replaceAll('\','/');
It doesn't convert this string

You need to escape the backslash \:
println yourString.replace("\\", "/")

You could also use Groovy's slashy string, which helps reduce the clutter of Java's escape character \ requirements. In this case, you would use:
Change_1 = Log_file_1.replaceAll(/\/,'/');
Slashy strings also support interpolation, and can be multi-line. They're a great tool to add to your expertise.
References
Groovy's syntax documentation
Baeldung's Groovy strings documentation

Related

Groovy: treat a string with interpolation placeholder as a regular string and don't interpolate

We're using a third party tool that uses groovy. We have two steps:
Construct a string dynamically -> ${p:test},${p:test2}
We interpolate the string which got constructed in the first step
However, no matter what I try, as soon as groovy detects ${, it treats if for interpolation. How can one get groovy to output '${p:test},${p:test2}' as a string without interpolating the values?
I tried so many things:
Escaping with \
Slashy strings
StringBuilder
<<=
Dollar slashy string
As long as I remove the '{' from the string, it is handled like a string. Even doing something like '$#p:test},$#p:test2}'.replace('#', '{') results in interpolation.

JMeter remove a single character from string variable

I am trying to remove "/" character from my specific string but I cannot succeed.
My example string:
F#S#3440642#SOW/AGT#10928415#F#-1#undefined#false
I need to remove that "/" character between "SOW" and "AGT" substrings. I tried some tricks with JSR223 but I couldn't do it; either get an error in my logs, or I cannot remove that character from the string.
Could you please help me with how to remove "/" character by using JSR223?
Just use String.replaceAll() function like:
vars.put('somevar', vars.get('somevar').replaceAll('/', ''))
Demo:
in the above snippet vars stands for JMeterVariables class instance, see JavaDoc for details and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.
Also be aware that there are __strReplace() and __strReplaceRegex() custom JMeter Functions so you don't need to do any Groovy scripting at all
You can use Java's String replace:
String token = "F#S#3440642#SOW/AGT#10928415#F#-1#undefined#false";
log.info(token.replace("/",""));

What is %s+ or %s used for in lua and how do you use it?

How do i use %s in lua, or a better question would be how is it used?
so here is what i have tried before assuming this is how it is used and how it works.
local arg1 = 'lmao'
print('fav string is %arg1')
at first i thought it was something used to reference a string or numeral inside of a string without doing like
print('hello '..name..'!')
Can someone provide me some examples or a explanation on how this is used and what for?
A % in a string has no meaning in Lua syntax, but does mean something to certain functions in the string library.
In string.format, % is used to make a format specifier that converts another argument to a string. It's documented at string.format, but that refers to Output Conversion Syntax and Table of Output Conversions to explain almost all of the specifier syntax.
The % is also used to designate a character class in the pattern syntax used with some string functions.
Here is your code using string.format:
local arg1 = 'lmao'
print(string.format('fav string is %s', arg1))
Or, taking advantage of the string metatable:
local arg1 = 'lmao'
print(('fav string is %s'):format(arg1))
Lua uses %s in patterns (Lua's version of regular expressions) to mean "whitespace". %s+ means "one or more whitespace characters".
Ref: https://www.lua.org/manual/5.3/manual.html#6.4.1

GString performance

If I need plain string in Groovy, does using double-quoted literals make any influence on performance?
For instance:
def plainString = 'Custom string'
def gString = "Custom string"
In my understanding, plain String should be faster because during runtime there are no searches for specific characters and substitutions.
From the Groovy Language Specification:
Double quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.
Therefore, feel free to use double quotes or single quotes: they will result in the same type of object. The difference will be when you have a $ in the double-quoted string. But by then, we are talking semantics, not performance.

How do you escape a semicolon in COMPILE_FLAGS property of cmake?

I need to get this as a result in the preprocessor definitions of the msvc generator:
MYPATH=\"d:\\;.\\Lib\"
But when I use the following escape sequence in set_source_files_properties:
set_source_files_properties(source.c PROPERTIES COMPILE_FLAGS "-DMYPATH=\\\"d:\\\;.\\\\Lib\\\"")
the generated result is: MYPATH=\"d:\";".\Lib\"
Note the double-quoted semicolon. Is there a quoting workaround to allow unquoted semicolons?
AFAIR, cmake treat ; as list separator, so it behaves in such way for properties as per documentation.
PROPERTY [value1 [value2 ...]
Probably you've better to try something like this - make it string variable and then try substitute it.
set(MY_PATH "\"d:\\\;.\\\\Lib\\\"")
set_source_files_properties(source.c PROPERTIES COMPILE_FLAGS ${MY_PATH})
HTH,
Sergey

Resources