I want to make an application to find differences between two strings. How do I solve this?
st = "this is a cat.this is my cat."
st1 = "this is cat. this my cat."
The output should be "a is" as the missing words.
Here is my code
@SuppressLint("DefaultLocale")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String st="this is a cat. this is my cat.";
TextView tv1=(TextView) findViewById(R.id.textView2);
String st1="This is cat. this my cat.";
String blank="";
if(st.toLowerCase().contains(st1.toLowerCase()))
{
st=st.toLowerCase().replace(st1.toLowerCase(), blank);
tv1.setText(st);
}
}
Сorrect solution is given below:
You could use StringUtils.differences, it's the source:
then
You could just implement this methods, or the entire library if you need more methods from this library.
Documentation here.
The easy way is to split the both strings on the bases of spaces. e.g
now you have two arrays. loop through them and find what is missing and what is not.
Alternatively you can use StringTokenizer class. Hope this will help.
You need a sort of DIFF function.
Check these answers: Extract the difference between two strings in Java
How to perform string Diffs in Java?