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 8955147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:28:56+00:00 2026-06-15T14:28:56+00:00

When we need an array of strings to be concatenated, we can use mkString

  • 0

When we need an array of strings to be concatenated, we can use mkString method:

val concatenatedString = listOfString.mkString

However, when we have a very long list of string, getting concatenated string may not be a good choice. In this case, It would be more appropriated to print out to an output stream directly, Writing it to output stream is simple:

listOfString.foreach(outstream.write _)

However, I don’t know a neat way to append separators. One thing I tried is looping with an index:

var i = 0
for(str <- listOfString) {
  if(i != 0) outstream.write ", "
  outstream.write str
  i += 1
}

This works, but it is too wordy. Although I can make a function encapsules the code above, I want to know whether Scala API already has a function do the same thing or not.

Thank you.

  • 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-06-15T14:28:58+00:00Added an answer on June 15, 2026 at 2:28 pm

    Self Answer:

    I wrote a function encapsulates the code in the original question:

    implicit def withSeparator[S >: String](seq: Seq[S]) = new {
      def withSeparator(write: S => Any, sep: String = ",") = {
        var i = 0
        for (str <- seq) {
          if (i != 0) write(sep)
          write(str)
          i += 1
        }
        seq
      }
    }
    

    You can use it like this:

    listOfString.withSeparator(print _)
    

    The separator can also be assigned:

    listOfString.withSeparator(print _, ",\n")
    

    Thank you for everyone answered me. What I wanted to use is a concise and not too slow representation. The implicit function withSeparator looks like the thing I wanted. So I accept my own answer for this question. Thank you again.

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

Sidebar

Related Questions

I have an array of DateTime objects and need to use them as strings
I have an array of strings in Javascript that I need to use to
I have an array of strings of random letters, and I need to know
I have an array of strings which I need to order from Z-A: Dim
I have one array of strings. I need to find all strings starting with
I have an array of strings that are fractions (1/3, 2/5) etc. I need
I have an array of strings which need to be taken into a map.
I have an array of strings I need to sort in JavaScript, but in
I have a string array in strings.xml, in the values I need to have
I have an array of strings . I need all the strings which do

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.