1

I am trying to find a way, how to compare two strings and upgrade the first, when the second has more in it. For example

String A="This is a statement!"; 
String B="This is a statement! Good luck!"; 
if(A<B{   //B has more letters
        //Upgrade A }
  else{ //Upgrade B
      }

I mean with upgrade not overwriting like A=B. My strings has usually much lines. I want to keep the value of the string and just insert the new things from the other String. Has someone an idea?

EDIT: Thank you for the nice answers. Unfortunetly I didnt exlain it more clear, sorry my fault. My problem is, that I do now know where the changes are, the strings could look like this:

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
3
  • 1
    What's upgrade? Also, what happens if the strings does not share a prefix? Note that Java does not have operator overloading, and even if it has, < will most likely compare the lexical ordering. Commented Jun 28, 2013 at 14:23
  • 1
    This code will not compile. Commented Jun 28, 2013 at 14:24
  • Write "String length Java" and you'll be amazed from the results! Commented Jun 28, 2013 at 14:24

11 Answers 11

2

I guess you need something like this:

String A="This is a statement!"; 
String B="This is a statement! Good luck!";

if (A.length() < B.length()){   //B has more letters
        A += B.subString(A.length(), B.length()-1);
} else{ 
        B += A.subString(B.length(), A.length()-1);
}

Hope this is what you are looking for :).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Of course this is the best solution for the problem I described by an oversight. I ll mark this as solution for sure.
1

How about this:

if(A.length() < B.length() {   //B has more letters
    //Upgrade A 
}
else { //Upgrade B

}

Comments

0

Use String.length() to get the length of a String.

Comments

0

Compare the Strings by length:

if (A.length() > B.length()) {
    B = A;
} else {
    A = B;
}

Comments

0

Try this

if(!B.contains(A)){
   A = B;
}

Comments

0
if (B.length() > A.length()) { // upgrade B as it's longer 
} else if (A.length() > B.length()) { // upgrade A as its longer
} else if (A.length() == B.length()) { // not sure what to do here as they're of equal length
}

Aside from null-checking, I believe this encompasses every possible case.

Comments

0

I mean you'll use a combination of subsString() and length(). Take b.subString(a.length, b.length-1) and concatenate that substring to a

Comments

0

Use String.length() to compare the size then concat the end of the longest chain.

String a="This is a statement!"; 
String b="This is a statement! Good luck!"; 

if(b.length() > a.length()) {
    a = a.concat(b.substring(a.length()));
}
else if(a.length() > b.length())
{
    b = b.concat(a.substring(b.length()));
}

Comments

0

Compare strings by length, using String.length() For example

public class Test{
   public static void main(String args[]){
      String Str1 = new String("This is a statement!");
      String Str2 = new String("This is a statement! Good luck!" );

     if(Str1.length() > Str2.length())
        Str2 += Str1;
     else
        Str1 += Str2;
}

Comments

0

Hope this helps.

if(A.contains(B) && !A.equals(B))
{
  A += B.substring(A.length(),B.length());
}

Comments

0
String A = "This is a statement!"; 
String B = "This is a statement! Good luck!"; 
if (B.length() > A.length()) { //B has more letters
    //Upgrade A
} else {
    //Upgrade B
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.