Concatenate Two Strings

Let's say I have a string obtained from a cursor,this way:

String name = cursor.getString(numcol);

and another String like this one:

String dest=cursor.getString(cursor.getColumnIndexOrThrow(db.KEY_DESTINATIE));

If finally I wanna obtain a String from the two of them,something like:

name - dest

Let say if name=Malmo and dest=Copenhagen

How could I finally obtain Malmo-Copenhagen???

Because android won't let me write :

name"-"dest
0

3 Answers

You need to use the string concatenation operator +

String both = name + "-" + dest;
2

The best way in my eyes is to use the concat() method provided by the String class itself.

The useage would, in your case, look like this:

String myConcatedString = cursor.getString(numcol).concat('-').
       concat(cursor.getString(cursor.getColumnIndexOrThrow(db.KEY_DESTINATIE)));

You can use concatenation operator and instead of declaring two variables only use one variable

String finalString =  cursor.getString(numcol) + cursor.getString(cursor.getColumnIndexOrThrow(db.KEY_DESTINATIE));
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest