Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:42:23+00:00 2026-05-27T10:42:23+00:00

I saw this answer of how to join String[] to comma separated string. However

  • 0

I saw this answer of how to join String[] to comma separated string.

However I need the util to join the string in the array only if the values are not empty.

What is the best way to do it? without looping before on the String[] to remove. I prefer one method that does both.

EDITED

for instance:

I, love, , u

will be:

 I love u
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:42:24+00:00Added an answer on May 27, 2026 at 10:42 am
    public class So20111214a {
        public static String join(String[] argStrings) {
            if (argStrings == null) {
                return "";
            }
            String ret = "";
            if (argStrings.length > 0) {
                ret = argStrings[0];
            } // if
            for (int i = 1; i<argStrings.length; i++) {
                ret += (argStrings[i] == null) 
                        ? "" 
                        : (argStrings[i].isEmpty() 
                            ? "" 
                            :  ( "," + argStrings[i] ) );
            } // for
            return ret;
        } // join() method
    
        public static void main(String[] args) {
            String[] grandmasters = {  
                "Emanuel Lasker", 
                "José Raúl Capablanca", 
                "Alexander Alekhine", 
                "Siegbert Tarrasch", 
                "Frank Marshall"  
            };
            String[] s1 = null;
            String[] s2 = {};
            String[] s3 = { "Mikhail Botvinnik" };
            System.out.println(join(s1));
            System.out.println(join(s2));
            System.out.println(join(s3));
            System.out.println(join(grandmasters));
            System.out.println(join(new String[]{"I", "love", "", null, "u!"}));
        } // main() method
    
        /* output:
        <empty string>
        <empty string>
        Mikhail Botvinnik
        Emanuel Lasker,José Raúl Capablanca,Alexander Alekhine,Siegbert Tarrasch,Frank Marshall
        I,love,u!
        */
    
    } // So20111214a class
    

    PS: Sorry for using the ? operator – I had to do it quickly, I am at work. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am 90% sure I saw this answer on stackoverflow before, in fact I
I saw this in an answer to another question , in reference to shortcomings
I saw this in a answer by Marc Gravell, and I just don't see
I saw this question asked about C# I would like an answer for PHP.
After reading this question , i saw the answer by Naveen containing a link
I saw this answer to a similar question , but it always scares me
I saw this answer that was given two years ago, but all I'm seeing
I saw this post on SO: UISearchBar solid color But no answer was provided.
In an answer on Stack Overflow, I saw this code: CREATE TABLE Favorites (
Sorry for noobness-level of the question. I saw this answer, where it says Add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.