Replace directory path by manipulating string - Java [duplicate] - java

This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 6 years ago.
How do i replace the directory path of a string?
The original string is:
String fn = "/foo/bar/ness/foo-bar/nessbar.txt";
i need the output to look like:
/media/Transcend/foo-bar/nessbar.txt
I've tried the line below but it doesn't work
fn.replaceAll("/foo/bar/ness", "/media/Transcend");

You forget to rewrite variable:
String fn = "/foo/bar/ness/foo-bar/nessbar.txt";
fn = fn.replaceAll("/foo/bar/ness", "/media/Transcend");

Try this:
fn = fn.replaceAll("/foo/bar/ness", "/media/Transcend");
The replaceAll method returns a new String object, leaving the original object unmodified. That's why you need to assign the result somewhere, for example, in the same variable.

replaceAll somehow did not work for me, could not figure out why, i ended up something like this, though it replaces only first part of path.
String fn = "C:/foo/bar/ness/foo-bar/nessbar.txt";
Strign r = "C:/foo/bar/ness";
fn = "C:/media/Transcend" + fn.substring(r.length());

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
...

How do I replace the " character in Java? [duplicate]

This question already has answers here:
What are all the escape characters?
(5 answers)
Closed 2 years ago.
I read a line from a txt file containing a string = "abc,"".
If one wanted to replace string to: string = "abc", one would write
string = string.replace(",",""); to replace the comma, but how would one replace the "?
Problem:
string = string.replace(""","");//code does not work because of """
You need to escape the ", so like this:
string = string.replace("\"","")

Only display part of string after a certain word in Java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
I am trying to only display data after a certain static word (in)
Example:
String jobName = job.getDescription();
returns the following:
XYZ/LMNOP in ABCEFG
I only want the data after the "in" in this scenario. However the XYZ/LMNOP is different in almost every case so I cannot simply call out that section of the string.
You can use split() in the String class.
String jobName = job.getDescription();
String[] parts = jobName.split("in"); { "XYZ/LMNOP", "ABCEFG" }
String before = parts[0]; // XYZ/LMNOP
String after = parts[1]; // ABCEFG
Find index of "in" in the string and then use the string from that particular index+3 to last.
int k = p.indexOf("in");
System.out.println(p.substring(k+3));
index+3 because "i", "n" , " " are 3 characters.
First you need to understand your strings possible data values. If it is always <some_text> in <some_text> then there are muliple ways as other users have mentioned.
Here is another way, whih is bit simpler
String[] strArray = job.getDescription().split(" "); //splits array on spaces
System.out.println(strArray[2]);
try using this
String search = " in ";
String d = job.getDescription();
d = d.substring(d.indexOf(search) + search.length(), d.length());
Outputs, given the inputs:
[find something in a string] -> [a string]
[finding something in a string] -> [a string] // note findINg, that does not match
The search key can be changed to simply in if desired, or left as is to match the question posted to avoid an accidental in in a word.
If you so choose, you can also use .toLower() on getDescription() if you want to be case insensitive when matching the word in as well.

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

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

Hints for java.lang.String.replace problem? [duplicate]

This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 7 years ago.
I would like to replace "." by "," in a String/double that I want to write to a file.
Using the following Java code
double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176
System.out.println(myDouble);
String myDoubleString = "" + myDouble;
System.out.println(myDoubleString);
myDoubleString.replace(".", ",");
System.out.println(myDoubleString);
myDoubleString.replace('.', ',');
System.out.println(myDoubleString);
I get the following output
38.1882352941176
38.1882352941176
38.1882352941176
38.1882352941176
Why isn't replace doing what it is supposed to do? I expect the last two lines to contain a ",".
Do I have to do/use something else? Suggestions?
You need to assign the new value back to the variable.
double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176
System.out.println(myDouble);
String myDoubleString = "" + myDouble;
System.out.println(myDoubleString);
myDoubleString = myDoubleString.replace(".", ",");
System.out.println(myDoubleString);
myDoubleString = myDoubleString.replace('.', ',');
System.out.println(myDoubleString);
The original String isn't being modified. The call returns the modified string, so you'd need to do this:
String modded = myDoubleString.replace(".",",");
System.out.println( modded );
The bigger question is why not use DecimalFormat instead of doing String replace?
replace returns a new String (since String is immutable in Java):
String newString = myDoubleString.replace(".", ",");
Always remember, Strings are immutable. They can't change. If you're calling a String method that changes it in some way, you need to store the return value. Always.
I remember getting caught out with this more than a few times at Uni :)

Categories

Resources