- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print all the elements of a String array in Kotlin in a single line?
In this article, we will take an example and show how to print all the elements of a String array in a single line using a Kotlin library class. In order to do that, we will use a String function called joinToString(), provided by the Kotlin library.
As per the Kotlin documentation, the function definition looks like this −
fun <T> Array<out T>.joinToString( // the String will be separated by this separator: CharSequence = ", ", // This will be added as prefix to the String prefix: CharSequence = "", // This will be added as postfix to the String postfix: CharSequence = "", // This number of element will be printed, // the remaining elements will be denoted but truncated sequence limit: Int = -1, truncated: CharSequence = "...", //any transformation required over the String transform: ((T) -> CharSequence)? = null ): String
This function takes several attributes in order to convert an arrayList into a String.
Example - Printing all elements of a String array in a single line
fun main(args: Array<String>) { val mylist = listOf("Jam", "bread", "apple", "mango") println( mylist.joinToString( prefix = "[", separator = "-", postfix = "]", truncated = "...", ) ) }
Output
On execution, it will produce the following output −
[Jam-bread-apple-mango]
- Related Articles
- Kotlin Program to Print a String
- Print All Distinct Elements of a given integer array in C++
- Java program to print all distinct elements of a given integer array in Java
- C# program to print all distinct elements of a given integer array in C#
- Python program to print all distinct elements of a given integer array.
- How to split a String into an array in Kotlin?
- How to detect and replace all the array elements in a string using RegExp in JavaScript?
- How to split a string into elements of a string array in C#?
- How to print a string two times with single statement in Python?
- How to convert list elements into a single string in R?
- Print all subsequences of a string in C++
- Print all permutations of a string in Java
- How to use for loop to print all the elements of a list in R?
- How to print all the characters of a string using regular expression in Java?
- Convert JavaScript array iteration result into a single line text string

Advertisements