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.

7
  • Isn't that code doing the opposite of what you actually want? Commented Aug 28, 2013 at 16:15
  • I didn't get you. @RohitJain Commented Aug 28, 2013 at 16:17
  • I mean, your code currently will add comma after all the elements except the first. But you want the opposite. Add comma only after first element. Check to see whether your current code is working fine. Commented Aug 28, 2013 at 16:18
  • @RohitJain Nope. It's worked and returned ABC,BCD,DEF,EFG Commented Aug 28, 2013 at 16:18
  • @RohitJain May be you didn't get me. Please check my last comment. That's the format I want. Commented Aug 28, 2013 at 16:19

7 Answers 7

1

How about this:

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

Output:

ABC, BCD, DEF, EFG
Sign up to request clarification or add additional context in comments.

2 Comments

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
I think the 'ABC','BCD' etc are just examples. The actual string may contain spaces that shouldn't be replaced.
0

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

Comments

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"));
    }
}

Comments

0

How about-

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

Output:

ABC, BCD DEF EFG

Comments

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.

Comments

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.

Comments

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)
{
    ...
}

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.