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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:50:50+00:00 2026-05-20T11:50:50+00:00

I have a List defined as: val l = List(1, 2, 3) I want

  • 0

I have a List defined as:

val l = List("1", "2", "3")

I want to convert it to the string

"1:2:3"

One way is the following:

l.foldLeft("")((x, y) => x + (if (x == "") "" else ":") +y)

Is there a more elegant method?

[EDIT: further explanation]

Easy Angel’s answer works when the elements of l have a ‘meaningful’ toString method.

although I have l as List[String], l can be a list of a custom type which does not override the toString method, say, as in:

class my(i:Int) {
  val x = i
}

I have also a method

def getMy(m:my) = "Hello "+m.x

So I would like the output of getMy to be used instead of the output of the default toString method.

  • 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-20T11:50:51+00:00Added an answer on May 20, 2026 at 11:50 am

    You can use mkString method of List:

    l mkString ":"
    

    More info can be found in scaladoc:

    http://www.scala-lang.org/api/rc/scala/collection/Iterable.html#mkString:String


    As alternative you can use reduceLeft for example:

    l.reduceLeft(_ + ":" + _)
    

    As an answer to your second question: Just combine it with map:

    l map getMy mkString ":"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: private lazy val keys: List[String] = obj.getKeys().asScala.toList obj.getKeys returns
I have the following function defined inside my linked list class. The declaration in
I have a list iteration in python defined like this: for i in range(5):
I have a class Bar that has a user-defined list of config keys and
If I have a List < Person > where person is defined by the
I have a List<ParentObject> where ParentObject is defined as: public class ParentObject { ChildObject
I have defined a custom list template type for SharePoint . I install it
I have a button defined inside of a DataTemplate for my list box. <phone:PhoneApplicationPage.Resources>
I have a table a with a list of id's, and a user-defined function
I have a Review Model like the one defined below (I removed a bunch

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.