alternative for strings combing strings that isnt "+" [duplicate] - java

This question already has answers here:
String variable interpolation Java [duplicate]
(5 answers)
Closed 1 year ago.
Is there any way to join a string and another variable together, that is also a string, without using the "+" operator. Specifically one similar to this one that is done in C#.
string str = $"Hello {userName}. Today is {date}.";
If there is any way to achieve a similar or same outcome in Java please let me know.

See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String.format("Hello %s. Today is %s.", userName, date);

Related

Java augment strings to string [duplicate]

This question already has answers here:
Java Equivalent to .NET's String.Format
(6 answers)
String replacement in java, similar to a velocity template
(12 answers)
Closed 4 years ago.
I need to augment some string like it's done by logback/famous loggers or property accessors.
String str= "I can {0} give {1} back your horses or restore your {2} to life.";
String[] got=new String[]{"not","you","dead"};
Are they any utility functions provided by any library that can convert str to replace {i} with indices in array?
Its most probably a duplicate and please refer me to some similar question.

Convert a string in code in Java [duplicate]

This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 5 years ago.
I'm trying to convert a string in code in Java, but i have no idea how to do it or if it is possible.
This is my Java code (Measure is an other class I have created)
String str= "Measure m = new Measure(10,1);";
Is it possible to run the code in the string?
No I dont think that is a good practice, you don't need to do it that way,
just instantiate outside of the string, it will be fine and good practice.
Measure m = new Measure(10,1);

Weird Java extended ascii error [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to do a comparison in Java with 2 strings containing a extended ASCII character.
boolean result = "éasdfasdf".substring(0,1).equals("é");
Can somebody explain why this results false? I think it has something to do with character encoding, but I can't figure out what exactly the problem is here...
Update: ideone.com does successfully run these 2 lines, so the problem is locally in my box. I think I found some more proof of that:
System.out.println("éb".charAt(1) == 'b');
Does also fails... Can it be the problem of 2 different character encodings?
Use
boolean result = "éasdfasdf".substring(0,1).equals("é")
And it will give expected result!The reason is simple - using '==' you compare objects by reference, not by value. So equals() solves this problem

Updating a string [duplicate]

This question already has answers here:
compare and update a string
(11 answers)
Closed 9 years ago.
I already opened a similar question, unfortunetly I didnt explain my problem correctly, sorry about that.
This time I hope everyone understand what I mean.
My intention is to Update a String A with a String B.
For example:
String A:
A
B
C//Good morning, sir
D//A comment
E
String B:
A
B//Yes
C
D
DD
E
The result should be:
A
B//Yes
C//Good morning, sir
D//A comment
DD
E
I do not know where the differences are, I just want to keep everything in String A and just add the new things from String B (to the correct positions).
Has anyone an idea if java can do this?
Well, how do you know where to insert the parts of your string? You could use an array and update write all words of your strings on an index by using a simple loop.

hi I want the code for &b=52&f=norefer and &b=52 [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
I want the pattern for removing the &b=128&f=norefer from following url
http://www.yahoo.com/url/5f&b=52&f=norefer
http://www.yahoo.com/url/6aa82d?show=all&page=2&b=52
String finalUrl =decodedUrl.replace("&b=52","");
page.setPageUrl(finalUrl);
I want to remove &b=52&f=norefer from the first url and &b=52 and from the second url which pattern i will use please give me the code without hard-coded value.
url.replaceFirst("&b=.*", ""); will remove the tail of the string starting from "&b=". I hope it is good enough for you. At least it is OK for your two exampels and is not sensitive to value of parameter b.
If it is not enough try to describe your task more specifically or just learn a little bit regular expressions.

Categories