Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate two strings

Tags:

string

android

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
like image 957
adrian Avatar asked Jun 05 '11 16:06

adrian


People also ask

How do you concatenate two strings give an example?

String s1 = new String("Hello"); //String 1. String s2 = new String(" World"); //String 2. String s = String.join("",s1,s2); //String 3 to store the result. System.out.println(s.toString()); //Displays result.


2 Answers

You need to use the string concatenation operator +

String both = name + "-" + dest;
like image 54
Jon Skeet Avatar answered Oct 01 '22 10:10

Jon Skeet


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)));
like image 31
keyboardsurfer Avatar answered Oct 01 '22 08:10

keyboardsurfer