How to get the new value of new replaced string? [duplicate] - java

This question already has answers here:
String replace method is not replacing characters
(5 answers)
replace String with another in java
(7 answers)
Closed 4 years ago.
Im still new at java. Is there a way to get the new string that have been replaced ?
import java.io.*;
public class Test {
public static void main(String args[]) {
String str = new String("wew");
System.out.println(str.replaceAll("w", "61"));
System.out.println(str.replaceAll("e", "31"));
}
}
output:
61e61
w31w
Desired new output:
613161
I want to get the output string 61e61 then replaced the e to 31

You can chain replaceAll as:
System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));
Currently, you're returning two different strings with both your print statements.
System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'

You're using it wrong. The method replaceAll of the Class String returns a String.
You have to use the return value again (which can be written in one line):
String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str);
Outputs: 613161

Let's review why your code doesn't provide what you expect.
You create an instance with "wew".
String str = new String("wew");
Then you replace "w" in "wew" with "61" and print the result "61e61"
System.out.println(str.replaceAll("w", "61")); //61e61
Now, the important part here is that the result of str.replaceAll is a new instance, so str is still "wew".
System.out.println(str); // wew
This explain why the second replacement will print "w31w"
System.out.println(str.replaceAll("e", "31")); //w31w
The reason is that String are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.
str = str.replaceAll("w", "61");
Now, the result is kept in the variable "str"
System.out.println(str); // 61e61
Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.
str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161
But if you want to print the intermediate result, you will need do it in two statement
str = str.replaceAll("w", "61");
System.out.println(str); // 61e61
System.out.println(str = str.replaceAll("e", "31")); // 613161
Note the last statement, it is possible to merge both assignment and print statement.
First str = .. will be evaluated then the result will be printed.

String.replaceAll() will return a new String so that your code System.out.println(str.replaceAll("w", "61")); and System.out.println(str.replaceAll("e", "31")); need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3:
StringUtils.replaceEach("wew", new String[]{"w", "e"}, new String[]{"61", "31"});

str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161

Related

Remove duplicate string in java

I have two different string, let's say:
s1 = "hello"
s2 = "helloworlde1"
I want to get result as worlde1.
Can anybody suggest me how to achieve this?
try this
s2=s2.replace(s1,"");
this will remove s1 from s2
Hope below small example will help you.
public class Test {
public static void main(String a[])
{
String s1="hello";
String s2="helloworlde1";
s2=s2.replace(s1, "");
System.out.println(s2);
}
}
If we assume that the duplicate string is always going to be in front of the other string e.g
s2="helloworlde1"
then you can use Java "substring" like this
string output = s2.substring(s1.length())
otherwise you need some other logic to locate where the duplicated string is before using the substring method

Returning the needed value in a class - Java

I am confused about returning the needed value. Here is a part of my code:
public class StrNum {
public static int getInt(String input) {
String str = new String(input);
int result;
if (str.startsWith("b")) {
str = str.substring(1);
result = Integer.parseInt(str, 2);
}
else if (str.startsWith("x")) {
str = str.substring(1);
result = Integer.parseInt(str, 16);
}
Now, what I need to return is result. When I write return result;, it asks me to initialize the variable (and I am aware that it hasn't been initialized). When I use return result inside of the if statements, Eclipse tells me that I have to return a value.
Where am I being stupid here? I would appreciate a good explanation.
What will the method getInt() return if Str doesn't start with neither "b" or "x"? That would be an error because result is not being initialized. You could solve this by intializing result with a value that you would like to return in that case:
int result = -1; // for example
Edit:
Since you want to use input to determine if the number will be parsed as binary or hexadecimal, I would recommend you to add an else statement to parse the number in base 10 as default:
if (...)
// ...
else if (...)
// ...
else
result = Integer.parseInt(Str);
Note:
Try to follow Java naming conventions. Use names like someVar for variables/methods and use names like SomeClass for classes.
It's not necessary to create a new string instance Str, unless you are going to use the original input later in the same method.
To create a String with the same content you can simply do
String str = input;
Try
int result = 0;
You must initialize variables before returning them or in other words do result = . The variable must always be initialized no matter what code path your application takes.
Your can either return the result from if and also from else, provided you are not doing additional calculations after the else block.
or simply initialize result = 0, it will change anyways before you return.

Can I use the replaceFirst method to look for a string pattern and replace it?

I have a question involving the Replace Method. I saw a question similar to this on here, but I tried to do the replaceFirst but it didn't work for me. Is there, any way I can use the replace method to change a string that results in: Helle, Werld; to get it to result to BE "Hello, World" using the replace method. Is there a way using the replaceFirst method for me to search for the sequence of "le" and replace it with "lo" and also change "We" to "Wo"?. Please see my code below:
public class Printer
{
/**Description: Replacement class
*
*
*/
public static void main(String[] args)
{
String test1Expected = "Hello, World!";
String newString1;
String test1 = "Holle, Werld!";
newString1 = test1.replace('o', 'e');
//Could I do: newString1.replaceFirst("le","lo);
System.out.println("newString1 = " + newString1);
//Output comes out to "Helle, Werld!"
}
}
You can do two regular expressions separatelt one after the other. Please try the following
newString1 = newString1.replaceAll("le", "lo").replaceAll("We", "Wo");

StringBuilder vs. String considering replace

When doing concatenating lots of strings, I have been recommended to do it using a StringBuilder as such:
StringBuilder someString = new StringBuilder("abc");
someString.append("def");
someString.append("123");
someString.append("moreStuff");
as opposed to
String someString = "abc";
someString = someString + "def";
someString = someString + "123";
someString = someString + "moreStuff";
which would result in the creation of quite a few Strings, as opposed to one.
Now, I need to do a similar thing, but instead of using concatenation I use the replace method of String as such:
String someString = SOME_LARGE_STRING_CONSTANT;
someString = someString.replace("$VARIABLE1", "abc");
someString = someString.replace("$VARIABLE2", "def");
someString = someString.replace("$VARIABLE3", "123");
someString = someString.replace("$VARIABLE4", "moreStuff");
To accomplish the same thing using StringBuilder, I have to do this, just for one replace:
someString.replace(someString.indexOf("$VARIABLE1"), someString.indexOf("$VARIABLE1")+10, "abc");
So my question is: "Is it better to use String.replace and have lots of extra Strings created, or to use StringBuilder still, and have lots of long winded lines such as the one above?"
It is true that StringBuilder tends to be better than concatenating or modifying Strings manually, since StringBuilder is mutable, while String is immutable and you need to create a new String for each modification.
Just to note, though, the Java compiler will automatically convert an example like this:
String result = someString + someOtherString + anotherString;
into something like:
String result = new StringBuilder().append(someString).append(someOtherString).append(anotherString).toString();
That said, unless you're replacing a whole lot of Strings, go for whichever is more readable and more maintainable. So if you can keep it cleaner by having a sequence of 'replace' calls, go ahead and do that over the StringBuilder method. The difference will be negligible compared to the stress you save from dealing with the sad tragedy of micro-optimizations.
PS
For your code sample (which, as OscarRyz pointed out, won't work if you have more than one "$VARIABLE1" in someString, in which case you'll need to use a loop), you could cache the result of the indexOf call in:
someString.replace(someString.indexOf("$VARIABLE1"), someString.indexOf("$VARIABLE1")+10, "abc");
With
int index = someString.indexOf("$VARIABLE1");
someString.replace(index, index+10, "abc");
No need to search the String twice :-)
Guess what? If you are running with Java 1.5+ the concatenation works the same with string literals
String h = "hello" + "world";
and
String i = new StringBuilder().append("hello").append("world").toString();
Are the same.
So, the compiler did the work for you already.
Of course better would be:
String j = "hellworld"; // ;)
As for the second, yeap, that's preferred, but should't be that hard, with the power of "search and replace" and a bit of regex foo
For instance you can define a method like the one in this sample:
public static void replace( String target, String replacement,
StringBuilder builder ) {
int indexOfTarget = -1;
while( ( indexOfTarget = builder.indexOf( target ) ) >= 0 ) {
builder.replace( indexOfTarget, indexOfTarget + target.length() , replacement );
}
}
And your code currently looks like this:
someString = someString.replace("VARIABLE1", "abc");
someString = someString.replace("VARIABLE2", "xyz");
All you have to do is grab text editor an trigger something like this vi search and replace:
%s/^.*("\(.*\)".\s"\(.*\)");/replace("\1","\2",builder);
That read: "take anything in parenthesis and that looks like a string literal, and put it in this other string".
And your code will look from this:
someString = someString.replace("VARIABLE1", "abc");
someString = someString.replace("VARIABLE2", "xyz");
to this:
replace( "VARIABLE1", "abc", builder );
replace( "VARIABLE2", "xyz", builder );
In no time.
Here's a working demo:
class DoReplace {
public static void main( String ... args ) {
StringBuilder builder = new StringBuilder(
"LONG CONSTANT WITH VARIABLE1 and VARIABLE2 and VARIABLE1 and VARIABLE2");
replace( "VARIABLE1", "abc", builder );
replace( "VARIABLE2", "xyz", builder );
System.out.println( builder.toString() );
}
public static void replace( String target, String replacement,
StringBuilder builder ) {
int indexOfTarget = -1;
while( ( indexOfTarget = builder.indexOf( target ) ) > 0 ) {
builder.replace( indexOfTarget, indexOfTarget + target.length() ,
replacement );
}
}
}
I would say go for using StringBuilder but simply write a wrapper that facilitates making the code more readable and thus more maintainable, while still maintaining efficiency. =D
import java.lang.StringBuilder;
public class MyStringBuilder
{
StringBuilder sb;
public MyStringBuilder()
{
sb = new StringBuilder();
}
public void replace(String oldStr, String newStr)
{
int start = -1;
while ((start = sb.indexOf(oldStr)) > -1)
{
int end = start + oldStr.length();
sb.replace(start, end, newStr);
}
}
public void append(String str)
{
sb.append(str);
}
public String toString()
{
return sb.toString();
}
//.... other exposed methods
public static void main(String[] args)
{
MyStringBuilder sb = new MyStringBuilder();
sb.append("old old olD dudely dowrite == pwn");
sb.replace("old", "new");
System.out.println(sb);
}
}
OUTPUT:
new new olD dudely dowrite == pwn
Now you can just use the new version that is one easy liner
MyStringBuilder mySB = new MyStringBuilder();
mySB.append("old dudley dowrite == pwn");
mySB.replace("old", "new"):
Instead of having long lines like that, you could just write a method for replacing parts of StringBuilder strings, something along the lines of this:
public StringBuilder replace(StringBuilder someString, String replaceWhat, String replaceWith) {
return someString.replace(someString.indexOf(replaceWhat), someString.indexOf(replaceWhat)+replaceWhat.length(), replaceWith);
}
May be the String Class internally uses
indexOf
method to find index of old string and replace it with new string.
And also StringBuilder is not threadsafe so it executes much faster.
If your string really is large and you're worried about performance I would recommend writing a class which takes your template text and a list of variables, then reads over the source string character by character and builds the result using StringBuilder. That should be the most efficient both in terms of CPU and memory usage. Also, if you are reading this template text from a file I wouldn't load it all into memory up front. Process it in chunks as you read it from the file.
If you're just looking for a nice way to build a string that's not quite as efficient as StringBuilder but more efficient than appending strings over and over you can use String.format(). It works like sprintf() in C. MessageFormat.format() is an option too but it uses StringBuffer.
There is another related question here: Inserting a Java string in another string without concatenation?
All guys' codes have a bug .try yourReplace("x","xy").It will loop infinitely
Jam Hong is correct - the above solutions all contain the potential to loop infinitely. I guess the lesson to take away here is that micro optimisations can often cause all sorts of horrible issues and don't really save you much. Still, be that as it may - here is a solution that will not infinite loop.
private static void replaceAll(StringBuilder builder, String replaceWhat, String replaceWith){
int occuranceIndex = builder.indexOf(replaceWhat);
int lastReplace = -1;
while(occuranceIndex >= 0){
if(occuranceIndex >= lastReplace){
builder.replace(occuranceIndex, occuranceIndex+replaceWhat.length(), replaceWith);
lastReplace = occuranceIndex + replaceWith.length();
occuranceIndex = builder.indexOf(replaceWhat);
}else{
break;
}
}
}
while it's true that micro optimizations can be problematic, it sometimes depends on the context, for instance, if your replace happens to run inside of a loop with 10000 iterations, your will see a significant performance difference from the "useless" optimizations.
in most cases however, it's best to err on the side of readability

Insert string in beginning of another string

How to insert a string enclosed with double quotes in the beginning of the StringBuilder and String?
Eg:
StringBuilder _sb = new StringBuilder("Sam");
I need to insert the string "Hello" to the beginning of "Sam" and O/p is "Hello Sam".
String _s = "Jam";
I need to insert the string "Hello" to the beginning of "Jam" and O/p is "Hello Jam".
How to achieve this?
The first case is done using the insert() method:
_sb.insert(0, "Hello ");
The latter case can be done using the overloaded + operator on Strings. This uses a StringBuilder behind the scenes:
String s2 = "Hello " + _s;
Other answers explain how to insert a string at the beginning of another String or StringBuilder (or StringBuffer).
However, strictly speaking, you cannot insert a string into the beginning of another one. Strings in Java are immutable1.
When you write:
String s = "Jam";
s = "Hello " + s;
you are actually causing a new String object to be created that is the concatenation of "Hello " and "Jam". You are not actually inserting characters into an existing String object at all.
1 - It is technically possible to use reflection to break abstraction on String objects and mutate them ... even though they are immutable by design. But it is a really bad idea to do this. Unless you know that a String object was created explicitly via new String(...) it could be shared, or it could share internal state with other String objects. Finally, the JVM spec clearly states that the behavior of code that uses reflection to change a final is undefined. Mutation of String objects is dangerous.
Sure, use StringBuilder.insert():
_sb.insert(0, _s);
You can add a string at the front of an already existing one. for example, if I have a name string name, I can add another string name2 by using:
name = name2 + name;
Don't know if this is helpful or not, but it works. No need to use a string builder.
private static void appendZeroAtStart() {
String strObj = "11";
int maxLegth = 5;
StringBuilder sb = new StringBuilder(strObj);
if (sb.length() <= maxLegth) {
while (sb.length() < maxLegth) {
sb.insert(0, '0');
}
} else {
System.out.println("error");
}
System.out.println("result: " + sb);
}
import java.lang.StringBuilder;
public class Program {
public static void main(String[] args) {
// Create a new StringBuilder.
StringBuilder builder = new StringBuilder();
// Loop and append values.
for (int i = 0; i < 5; i++) {
builder.append("abc ");
}
// Convert to string.
String result = builder.toString();
// Print result.
System.out.println(result);
}
}
It is better if you find quotation marks by using the indexof() method and then add a string behind that index.
string s="hai";
int s=s.indexof(""");

Categories