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.
Related
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);
This question already has answers here:
Immutability of Strings in Java
(26 answers)
Closed 3 years ago.
I realize that the hashCode of the variable name, is different after the "update", but objectively what makes a String object in fact immutable ?
public static void main(String[] args) {
String str = "AB";
System.out.println(str ); // AB
str = str .replace(str .charAt(0) ,'W');
System.out.println(str );//WB
}
EDIT 1 : The hashCode is based on the value of the variable and have no relation with memory adress.
EDIT 2 : I now understand that Strings are references and not Objects in it self.
I read back all the answers for this same question and found out good answers in topics like [this] (Immutability of Strings in Java). Thank you whos tried to help me and my excuses for any silly misunderstood.
I also recommend this articles here to who wants better understand how Strings works in Java :
https://www.pushkarrajpujari.com/article/strings-in-java/
and how references works :
https://javaranch.com/campfire/StoryPassBy.jsp
EDIT 3: I cannot DELETE this topic anymore, according with Stackoverflow "You cannot delete this question as others have invested time and effort into answering it." which I agree.
If you look at the documentation of replace(), it mentions:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Therefore, the replaced String is an entirely new String.
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);
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 7 years ago.
I am doing a "Rock,Paper,Scissors" game with Java and I have a question, is there any way to create and Array with answers and check if any of these values equal to another array? For example:
Here is the answerArray (also I want to have short cuts for answers):
String[] answersArray = {"Rock","R","Paper","P","Scissors","S"};
And here is a randomArray for computer:
String[] npcAnswer = {"Rock","Paper","Scissors"};
int idx = new Random().nextInt(npcAnswer.length);
String npcRandomAnswer = (npcAnswer[idx]);
So, I want to check through the scanner if my answer (answersArray) equal to npcRandomAnswer.
Sorry if I have grammar mistakes, I did my best to explain my point.
Thank you.
You can only compare apples with apples. Or actually you can compare apples with oranges, but then you don't really need to compare them, you know the answer will always be false.
It looks like you don't really know yet what will your algorithm be.
I suggest you to concentrate on the algorithm, write pseudo code, and when you have it, try to make java out of it. If you'll have problem with the java, we can easily help you if you post your algorithm (in a new question)
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.