Algorithm for joining e.g. an array of strings

后端 未结 16 1769
陌清茗
陌清茗 2021-01-01 21:40

I have wondered for some time, what a nice, clean solution for joining an array of strings might look like. Example: I have [\"Alpha\", \"Beta\", \"Gamma\"] and want to join

相关标签:
16条回答
  • 2021-01-01 21:56

    In Java 5, with unit test:

    import junit.framework.Assert;
    import org.junit.Test;
    
    public class StringUtil
    {
        public static String join(String delim, String... strings)
        {
            StringBuilder builder = new StringBuilder();
    
            if (strings != null)
            {
                for (String str : strings)
                {
                    if (builder.length() > 0)
                    {
                        builder.append(delim);
                    }
    
                    builder.append(str);
                }
            }           
    
            return builder.toString();
        }
    
        @Test
        public void joinTest()
        {
            Assert.assertEquals("", StringUtil.join(", ", null));
            Assert.assertEquals("", StringUtil.join(", ", ""));
            Assert.assertEquals("", StringUtil.join(", ", new String[0]));
            Assert.assertEquals("test", StringUtil.join(", ", "test"));
            Assert.assertEquals("foo, bar", StringUtil.join(", ", "foo", "bar"));
            Assert.assertEquals("foo, bar, baz", StringUtil.join(", ", "foo", "bar", "baz"));
        }
    }
    
    0 讨论(0)
  • 2021-01-01 21:59

    Perl 6

    sub join( $separator, @strings ){
      my $return = shift @strings;
      for @strings -> ( $string ){
        $return ~= $separator ~ $string;
      }
      return $return;
    }
    

    Yes I know it is pointless because Perl 6 already has a join function.

    0 讨论(0)
  • 2021-01-01 21:59

    I wrote a recursive version of the solution in lisp. If the length of the list is greater that 2 it splits the list in half as best as it can and then tries merging the sublists

        (defun concatenate-string(list)
           (cond ((= (length list) 1) (car list))
                 ((= (length list) 2) (concatenate 'string (first list) "," (second list)))
                 (t (let ((mid-point (floor (/ (- (length list) 1) 2))))
                       (concatenate 'string 
                                    (concatenate-string (subseq list 0 mid-point))
                                    ","
                                    (concatenate-string (subseq list mid-point (length list))))))))
    
    
    
        (concatenate-string '("a" "b"))
    

    I tried applying the divide and conquer strategy to the problem, but I guess that does not give a better result than plain iteration. Please let me know if this could have been done better.

    I have also performed an analysis of the recursion obtained by the algorithm, it is available here.

    0 讨论(0)
  • 2021-01-01 22:00

    Use the String.join method in C#

    http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

    0 讨论(0)
  • 2021-01-01 22:01

    ' Pseudo code Assume zero based

    ResultString = InputArray[0]
    n = 1
    while n (is less than)  Number_Of_Strings
        ResultString (concatenate) ", "
        ResultString (concatenate) InputArray[n]
        n = n + 1
    loop
    
    0 讨论(0)
  • 2021-01-01 22:03

    join() function in Ruby:

    def join(seq, sep) 
      seq.inject { |total, item| total << sep << item } or "" 
    end
    
    join(["a", "b", "c"], ", ")
    # => "a, b, c"
    
    0 讨论(0)
提交回复
热议问题