How to change one character to string in string variable [duplicate] - java

This question already has answers here:
replace String with another in java
(7 answers)
Closed 8 years ago.
I have string "2x+3" and I want change 'x' to string "8".
String operation = "2x+3";
String x = "8";
And I want result such as "2*8+3"
or
String operation = "x+3";
String x = "8";
And I want result such as "8+3"
Does anyone know how to solve ?

operation = operation.replace("x","8")

Well you can't change the same string, because strings are immutable objects in java. What you can do is use the replace function in string class and get a new string out of it.
Have a look at this question : replace String with another in java

Related

split string and set values to class variable in java 8 [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 2 years ago.
String decodedChecksum="A01046085|T98494055e|1200|2020-05-31T06:12:46.365Z"
String[] splitArray = decodedChecksum.split("|");
/* here i want to set values to getter setter*/
{
sample.setAppNo(A01046085)
sample.setId(T98494055e)
..
}
Please provide solution to iterate the array and set valuease to varibale
It heavily depends on how your code is structured. If you're 100% sure of the structure of that string, it's as easy as:
String decodedChecksum="A01046085|T98494055e|1200|2020-05-31T06:12:46.365Z"
String[] splitArray = decodedChecksum.split("\\|");
sample.setAppNo(splitArray[0]);
sample.setId(splitArray[1]);
...
You need to escape the | because it's a special symbol in regex.
String decodedChecksum="A01046085|T98494055e|1200|2020-05-31T06:12:46.365Z";
String[] splitArray = decodedChecksum.split("\\|");
sample.setAppNo(splitArray[0]); // splitArray[0] = A01046085
sample.setId(splitArray[1]); // splitArray[0] = T98494055e
...

Java - Make string not case sensitive [duplicate]

This question already has answers here:
How do I convert strings between uppercase and lowercase in Java?
(6 answers)
Closed 6 years ago.
Here is a string
String foxes = "the,quick,brown,fox,jumped,over,the,lazy,dog";
I was wondering (although it is currently one in lower case)
What code would i use to make any string into Just lower or upper case
Many thanks
What about the String toUpperCase() Method ?
String newString = foxes.toUpperCase()
See http://www.tutorialspoint.com/java/java_string_touppercase.htm
There's a method in the String class:
String foxes = "the,quick,brown,fox,jumped,over,the,lazy,dog";
String upperCase = foxes.toUpperCase();
String lowerCase = foxes.toLowerCase();
String is immutable, so you don't alter the original; you create new ones.

getting the string value before dot in string through java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a string named extractDb values can be
GDSHKG.db
GDSMNH.db
GDSTKY.db
GDSLDN.db
now i want to remove the .extension partof it
that is i want to see the result as
GDSHKG
GDSMNH
GDSTKY
GDSLDN
please advise how to achieve this in java
Try to use substring like this:
String myString = "GDSHKG.db";
String newString = myString.substring(0, myString.indexOf("."));
System.out.print(newString);
Output:
GDSHKG

Converting from CharSequence to String in Java [duplicate]

This question already has answers here:
How to convert CharSequence to String?
(6 answers)
Closed 2 years ago.
Suppose I want to convert a CharSequence to a String in Java.
Which option (1 or 2) is better and why?
CharSequence source = "some text";
String someText1 = (String)source; // 1
String someText2 = source.toString(); // 2
The best option is behind Door #3:
String someText = String.valueOf(source);
Because that will handle the case where source is null.
#1 doesn't even work for CharSequence implementations other than String, so #2 is really your only option.

Java - simple string issue [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
string split in java
I have a string:
"jack#james#joahn#logan#"
I know there is a method of the String class that can seperate it on the symbol #. So it will return to me
"jack" "james" "joahn" "logan"
What is the name of this method?
This is String.split("#").
For example:
String s = "jack#james#joahn#logan#";
String parts[] = s.split("#");
See the String.split method. Here is an example:

Categories

Resources