Java string format in variable like Python [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In Python, below code can be run:
str = "i = %s"
print(str % i)
or
str = "i = {}"
print(str.format(i))
I know that Java can format string using
String str = String.format("i = %s", i);
But, I want to separate "i = %s" and i to make "i = %s" variable and use it on other codes, like below:
String pathFormat = "/v2.0/%s/search";
System.out.println(String.format(pathFormat, userId);
Is it possible in Java?

Yes, you can do that in Java just as in your last example.
String numberFormat = "i = %s";
int i = 17;
System.out.println(String.format(numberFormat, i));

Related

How to get the count of specific letters in a String? (Android) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I need to count the number of times a letter's present in a String.
For example:
str = "/data/name/data/name"
How do we get the number of / in this string?
val count = str.count { it == '/' }
To be honest, I am not sure whether you need an answer in java or kotlin (your tags include both), so if you need an answer in java:
String input = "/data/name/data/name";
char search = '/';
long count = input.chars().filter(ch -> ch == search).count();
(and if you need a kotlin version, just take a look at #Ivo's answer)
I think you can count with this way,
val str = "/data/name/data/name"
var count = 0
str.forEach {
if(it == '/'){
count++
}
}

When I convert a string to an integer how can I make it so I can multiply it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So basically what I did is I converted the string to an integer and now I want to make it so whatever number the user enters will be multiplied by 7.
String strName;
strName=this.txtInputDogName.getText();
String strAge;
strAge=this.txtInputDogAge.getText();
int foo;
foo = Integer.parseInt(strAge);
int strAge = (strAge*7);
this.lblOutput.setText("Hello "+strName+"'s age in human years is "+strAge);
You don't need to define and assign to a variable on separate lines.
Instead of writing
String strName;
strName=this.txtInputDogName.getText();
You can write it like this:
String strName = this.txtInputDogName.getText();
And you dont need the variable foo at all. You can just use parseInt, multiply it by 7, and convert it to string.
Now the improved and correct version of your code is:
String strName = this.txtInputDogName.getText();
String strAge = this.txtInputDogAge.getText();
strAge = Integer.toString(Integer.parseInt(strAge) * 7);
this.lblOutput.setText("Hello " + strName + "'s age in human years is " + strAge);

check regex for integers bigger than the negative -328 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for a regex that is able to match numbers bigger than -328, and if it is possible to provide another solution to match the same pattern but without the zero. I tried many things but still not sure about how it works, for example, ^\-?[0-9]\d{3,}$
I'm using it with the com.jfoenix.validation.RegexValidator in order to check the pattern in a textfield.
Thanks
Try this.
String pat = "^-(32[0-7]|3[0-1]\\d|[1-2]\\d\\d|\\d{1,2})|\\d+$";
for (int i = -1000; i <= 1000; ++i) {
String s = Integer.toString(i);
boolean result = s.matches(pat);
if (result != (i > -328))
System.out.println(i + " fail!");
}

Create test which add country code in a loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to create a test which will have base URL (https://www.horisen.com) and added part with country code (i.e https://www.horisen.com/se). Problem for me is there are 12 countries to be changed.
I tried to create if loop without any success.
So, in summary, I have to go through all 12 different languages, open those pages in current languages, and continue with next lang. I suppose that I need an array of 12 country codes and call that in a loop, but do not know how to acchieve this.
Thank you in advance
String url = htps://www.horisen.com/
int[] array;
array = new int[6];
array[0] = de;
array[1] = se;
array[2] = dk;
array[3] = fr;
array[4] = en;
array[5] = fi;
for(int i=0; i++) {
do not know how to add after string url country code on the ned
}
Next time, please put some actual java code (that compiles) in your example. Perhaps you're looking for something like this:
String url = "https://www.horisen.com/";
String[] countryCodes = {"de", "se", "dk", "fr", "en", "fi"};
for (String countryCode : countryCodes) {
String countryUrl = url + countryCode;
System.out.println(countryUrl);
}

Regular expression for splitting a String and getting the last part [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to split a String which is a file's name (ex: DIS_LU0786738343_20170608.pdf, KID_AMEV_20170608.pdf, Evolution-DIS_20170512.csv, Offres-invest_20170608.csv, OST_20170608.csv) to get the last part.
For example for the String DIS_LU0786738343_20170608.pdf I want to get only 20170608 which will be transformed on a Date object.
I've been googling this and I found a solution using Regular Expressions:
String regex = "some regex expression";
String fileName = "DIS_LU0786738343_20170608.pdf";
System.out.println(Arrays.toString(fileName.split(regex)));
This will return an Array like this : [DIS_LU0786738343_, 20170608]
So anyone can help me make the Regular Expression to do so ?
String fileName = "DIS_LU0786738343_20170608.pdf";
int startIdx = fileName.lastIndexOf("_");
int endIdx = fileName.lastIndexOf(".");
String finalStr=fileName;
if (startIdx > 0 && endIdx > 0)
finalStr = fileName.substring(startIdx + 1, endIdx);
System.out.println(finalStr);
I think this should work for you.
I dropped down the RegEx thing and found another solution that I want to share with anyone who'll need it in the future :
private LocalDate getDateFromPriipsDoc(File file) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String fileName = FilenameUtils.getBaseName(file.toString());
String dateInString = fileName.substring(fileName.lastIndexOf("_") + 1);
return LocalDate.parse(dateInString, formatter);
}
Do not hesitate to share another solution with RegEx ;)

Categories