0

First of all, I am not a experienced java developer. The problem is My sql query give return data like this ABC BCD DEF EFG. But, I want to add , after the every entry So, I was tried with this

if (cursor != null) {
            cursor.moveToFirst();
            do {

                if (cursor.getPosition() == 0) {

                    getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));

                } else {

                    getName = getName + ","
                            + cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
                }

            } while (cursor.moveToNext());
        }

My question is is it okay or is there any way to write this code efficiently(Like achiving the same goal by writing less line of code). Thank you.

user2579475
  • 1,051
  • 3
  • 11
  • 20

7 Answers7

1

How about this:

myString = "ABC BCD DEF EFG";
String newString = myString.replace(" ", ", ");
System.out.println(newString);

Output:

ABC, BCD, DEF, EFG
Pete B.
  • 3,188
  • 6
  • 25
  • 38
  • 1
    Reading his comments, I think he would want `replaceAll`, but this is the best answer to achieve what he's asking for IMO so +1 – StormeHawke Aug 28 '13 at 16:28
  • I think the 'ABC','BCD' etc are just examples. The actual string may contain spaces that shouldn't be replaced. – Moob Aug 29 '13 at 08:26
0

How about including the comma using the MySql query itself? You can find more about it in this link.

Community
  • 1
  • 1
Ramesh
  • 765
  • 7
  • 24
  • 52
0

The following code would be shorter and should achieve the exact same thing you posted.

if (cursor != null) {
    cursor.moveToFirst();
    getName = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
    while (cursor.moveToNext()) {
        getName = getName + "," + cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
    }
}
Mirco Widmer
  • 2,139
  • 1
  • 20
  • 44
0

How about-

String str = "ABC BCD DEF EFG";
str = str.replaceFirst(" ", ", ");
System.out.println(str);

Output:

ABC, BCD DEF EFG

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
0

You can do it in less code but its probably no more efficient:

if (cursor != null) {
    cursor.moveToFirst();
    while(cursor.moveToNext()) {
        getName += ((!"".equals(getName))?",":"") + cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
    } 
}

Depending on your requirements you could use a concat in the actual query.

Moob
  • 14,420
  • 1
  • 34
  • 47
0

Your code will be a bit more efficient if you use StringBuilder rather than String when building up your result.

StringBuilder str = new StringBuilder();
loop...
    if (first) {
        str.append(cursor.getString(...));
    } else {
        str.append(',');
        str.append(cursor.getString(...));
    }
String theString = str.toString();

Every time you use + with strings, behind the scene you are creating temporary objects that need to be garbage collected. By explicitly using a StringBuilder you completely avoid extra objects to build your string in each iteration of your loop.

Rob
  • 6,247
  • 2
  • 25
  • 33
0

I would like to point out one issue in your code ( not related to the string formatting ). When the cursor points to the first record, you should always check for null.

if (cursor.moveToFirst() != null)
{
    ...
}
sjdutta
  • 376
  • 1
  • 5