Fast variable insertion into string - java

Is there any Intellij Idea hotkey or a plugin to quickly insert variable into string?
e.g. i have string
"My code is working, yay! Result is = ",
and i have to add construction "+variable +", resulting in
"My code is working, yay! Result is = "+ variable +"."
to properly insert a variable.
When i have to insert variables into 20+ string, its driving me nuts :)
tried to find solution on google or plugin repository, no result
PS: i know about soutv hotkey, but it cant help me since i have to change already existing strings in code

Added Live Template
Abbreviation: ++
Description: Insert variable into string
Expand with: Enter
Template text: "+ $EXPR$ +" (do not forget double quotes)
Edit Template Variable:
expression:variableOfType("") default Value: "expr"
settings screen
You type ++ in a string, press Enter, and template text is added, and you only need to choose a variable. Works like a charm.
Thanks for the idea, #Meo

Related

Automatic `log.info` completion in IntelliJ IDEA?

I am using Java and I log things like:
log.info("createArticle userId={} articleId={} title={} content={}", userId, articleId, title, content);
As you can see, when inputting this line into my IDE, I have to manually write down those characters like "userId={} ". Ideally, I hope I can simply type the userId, and Intellij IDEA will automatically help me fill out both "userId={} " (in the format string) and , userId (in the arguments list).
Question: How can I do that in IntelliJ IDEA? Or, is there any other ways to type such logging lines faster?
Thanks for any suggestions!
It is possible to implement it with Live Templates and groovyScript for one of the variables: take the second segment, split it by comma, concat in something like part=${part}:
groovyScript("_1.split(',').collect { it.trim() + '={}' }.join(' ')", B)
See video

Wrong argument type for formatting argument

I use some codes like temp_out.setText(response.body().getCurrent().getTemp() + " ℃"); to get data from a weather API, but got two lint errors:
Do not concatenate text displayed with set text. Use resource placeholders only.
String literals in setText can not be translated. Use Android resources instead.
So i searched here for similar errors on this site and partly found a solution here https://stackoverflow.com/a/35053689/13899010,
I used this one because I didn't want using resource String to change the data displayed. Added this to String.xml:
<string name="blank">%d</string> then Changed my code to this:
temp_out.setText(getString(R.string.blank, response.body().getCurrent().getTemp() + " ℃"));
Now i get this error Wrong argument type for formatting argument '#1' in blank: conversion is 'd', received string (argument #2 in method call).
I saw similar question Android Studio "Wrong argument type for formatting Error" in String.format() but his solution didn't work for me. How to fix this please?
make sure response.body().getCurrent().getTemp() returning a int, if its a string use %s in string blank
you can try one of the following
<string name="blank">%d Celsius</string>
//invoking as follows
getString(R.string.blank, response.body().getCurrent().getTemp())
temp_out.setText("${response.body().getCurrent().getTemp()} Celsius")

how to join a string and variable to th:src

I want spring to search an image under path: /upload-dir/ID.jpg
ID is a variable that depends on the object's id.
Ive made something like this:
th:src="|#{/upload-dir/} ${dog.id} .jpg|"
but this is not working at all. I beg for any advice how to make it work :)
You can use the following structure to do so.
th:src="#{'/upload-dir/' + ${dog.id} + '.jpg'}"

Using select.getOptions() with Selenium to get values

I am using selenium to verify a web site where doctors' names appear in a drop-down box. If the name in the database has extra spaces (like "Kildare , Jack , MD") the blanks should be removed like "Kildare, Jack MD". I am using the selenium method select.getOptions() (where select is the web element of a select box).
When I retrieve the date from the box and print it, it shows the names as still having spaces (see Alois Alzheimer and Sigmund Freud below). In the text box visually it shows Sigmund Freud formatted correctly, but Alois Alzhemier still has a space after Alois (see the attached picture). I am not sure what is happening here. Looks like the select box occasionally removes blanks itself and sometimes doesn't. Any clues?
Here is the debug text from select box:
DEBUG [PP21644] ----<Alzheimer, Alois , DO>-----
DEBUG [PP21644] ----<Freud, Sigmund Schlomo, Jr, MD>-----
DEBUG [PP21644] ----<Mayo, Charles Mayo, MMIN>-----
And the picture of how it looks is attached. Sigmund is formatted correctly but Alois is not (see the space highlighted with a yellow square). And yes, I had to run this with famous doctors' names to protect the names of the actual providers.
There are a few ways you could do this
You could examine the HTML of the OPTIONs and figure out where the spaces are coming from, parse it, etc. to get rid of the spaces. I can't really offer code here since I don't know what the HTML looks like.
Another way given what you provided is to use regex and .replace() to remove repeated spaces and to remove a space before a comma.
public static void main(String[] args)
{
String s1 = "Alzheimer, Alois , DO";
String s2 = "Freud, Sigmund Schlomo, Jr, MD";
String s3 = "Mayo, Charles Mayo, MMIN";
System.out.println(cleanup(s1));
System.out.println(cleanup(s2));
System.out.println(cleanup(s3));
}
public static String cleanup(String s)
{
return s.replaceAll(" +", " ").replace(" ,", ",");
}

Get highlighted variable -Eclipse

I am implementing an Eclipse plugin for C programmers.
I can't find a way to get the variable that the user already highlited.(The user will highlight a variable in the editor and I need to know which variable it is/variable's name/location of this variable in the editor, like line number..)
Can anyone help to achieve that ?
Well after searching in some links, I achvieved that by using the ISelectionProvider and ITextSelection Interfaces. Here a code to get the name of the highlighted variable :
ISelectionProvider selProvider = textEditor.getSelectionProvider();
ITextSelection txtSel = (ITextSelection) selProvider.getSelection();
String varName = txtSel.getText();

Categories