How to replace parameters with 2 strings [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
The method accepts two strings s1,s2 as parameters and returns the string in the object that is triggered when each instance of string s1 is replaced by s2. otherwise, it will do nothing
for example :
if we have string:"abcdecc" , with parameters s1="cc" and s2="fff"
then the method will return string as : "abcdefff"
replaced s1 with s2

If you have (or can include) Apache Commons library in your project, then I recommend using StringUtils
String result = StringUtils.replace("abcdecc", "cc", "fff");
You can use String.replace() as well but it uses Regex, so in terms of performance StringUtils.replace() is better.

Simply use this as your method:
String replace_method(String s, String s1, String s2){
return s.replace(s1, s2);
}

Related

java: string split loose last element [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
im sorry for the screen shot
the strange result for me,
while result of split does not contain the last element,
from my pov the correct result must be
['[','xtrue','']
am i right?
public static List<String> splitString(String source, String delimiter) {
if (Objects.equals(delimiter, "[")) {
return Arrays.asList(source.split("\\["));
}
String[] sArr = source.split(delimiter);
return Arrays.asList(sArr);
}
sure, guess im not safe with split operator, but a little search on google do not solve my question how to use for get as i want
A per documentation:
Trailing empty strings are therefore not included in the resulting array.
So the output is correct.
If you want trailing empty strings you'll have to use the two-parameters version of split passing a negative integer as the second parameter, since
If the limit is negative then the pattern will be applied as many times as possible and the [resulting] array can have any length
So, like you say in your own answer
source.split(delimiter, -1);
will include the empty string after the last " .
for the community
the solution for my case
source.split(delimiter, -1);
thx

How to compare first letter of two strings by alphabetical order [Java] [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to return true if the first letter of a string alphabetically precedes the first letter of another string. So if s1 = "Adam" and s2 = "Bob" then s1.someMethod(s2) should return true since A comes before B in the alphabet. I just need a few lines to do this so maybe using charAt(0) first could be involved.
Cheers.
You can do the following:
1. Convert the strings to lowercase
2. Compare their ASCII values of the first characters
int diff=s1.charAt(0)-s2.charAt(0);
3. Print the result
if(diff>0)
return true;
else
return false;
s1.someMethod(s2) should return true
If you are already looking for a simple method, then String already implements Comparable interface, so you can simply use compareTo as shown below:
s1.compareTo(s2)<0
This will return true if s1 comes before s2 i.e., in the alphabetic order.
Also, just to add, s1.compareTo(s2) returns 0 if both the strings are equal.

When to use concat and append? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I started learning java and always had this bugging my mind.
When to use concat() and when to use append() operations.
Do they perform the same operation and what of their return types?
concat():
String has a concat method, string is immutable.
adds a string to another string.
It will create the new object after concatenation is done, since it is a immutable.
append():
StringBuilder and StringBuffer has append method, these two are mutable.
appends a char or char sequence to a string.
It will not create a new object, since it is a mutable one.

How search a datum in ArrayList<StringBuilder> with indexOf [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How search a datum in ArrayList with indexOf
ArrayList<StringBuilder> state = new ArrayList<StringBuilder>();
state.add(new StringBuilder("A"));
state.add(new StringBuilder("B"));
state.add(new StringBuilder("C"));
state.add(new StringBuilder("D"));
System.out.println(state.indexOf(new StringBuilder("B"))); //Out: -1
You are using an ArrayList of StringBuilderobjects.
As StringBuilder is used for building strings, and not as actually Strings , you can't use indexOf(s), as the whole object itself should be equal to the one you are comparing so that it returns true.
In order to solve this problem, you should build the String however you want with the StringBuilder, and make an ArrayList of already-built Strings only, like this:
ArrayList<String> state = new ArrayList<String>();
StringBuilder a = new StringBuilder("A")
//Do whatever you want to a
state.add(a.toString());
state.add(b.toString());
state.add(c.toString());
state.add(d.toString());
.....
System.out.println(state.indexOf("B")); //Out: 1
This is because it searches by reference and you want to search by the contents of the string builder.
boolean there = state.stream() // use streams API
.anyMatch(sb -> sb.toString().equals("B")); // check string equals
System.out.println(there); // print if it's there

Where should the string literals during the checking for equality, left-side or right-side? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I used in my code "".equals(val) but if it is "".equals("abc")
then, what should be the correct expression for it.
Having a string literal on left-hand side of expression will not generate the NullPointerException.
if(a !=null && a.equals("value")) you can replace with if("value".equals(a)).
PS
Having the constant on the left-hand side is cleaner approach as prevent from making errors. It is can be called as yoda condition.
"abc".equals("def")
is equivalent to
"def".equals("abc")
if that's what you are asking...
Comparing two string literals? Just use the boolean constant, you know they're the same (or not) already.
Comparing a string literal and a string variable? literal.equals(variable). This prevents null pointer exception.
Two variables? Check for null, then nullCheckedVariable.equals(otherVariable).

Categories

Resources