List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)

When processing text (especially text files in patterned format), split which divides a character string into a list by a specified character and join which joins a list by a specified character into a character string I personally think that there will be a big difference in working time depending on whether or not you can master it, but since each language has a slight habit, I tried to summarize the basic usage.

language version OS
Perl 5.26.1 Ubuntu 18.04
PowerShell 5.1.17763.316 Windows 10 home
Java 1.8.0_131 Windows 10 home
Kotlin 1.3.21 Android 7.0
Python2 2.7.15 Ubuntu 18.04
Python3 3.6.7 Ubuntu 18.04

The version is that of the compiler / interpreter. The OS is the execution environment.

--Edit history --2019.05.14: Changed Kotlin code to Android independent

split

The example splits a comma-separated string with spaces. Regular expressions can be used in any language, but Kotlin / Python requires the use of a separate library.

Perl

#!/usr/bin/perl

my $string = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama";
my @item = split /,\s*/, $string;

for my $i (@item) {
    print $i, "\n";
}

Execution result

$ ./split.pl
Motomachi-Chukagai
Nihon Odori
Bashamichi
Minatomirai
Yokohama

PowerShell

$string = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama"
$item = $string -split ",\s*"

foreach ($i in $item) {
    Write-Output $i
}

Execution result

PS C:\Users\zaki\src\share> .\split.ps1
Motomachi-Chukagai
Nihon Odori
Bashamichi
Minatomirai
Yokohama
PS C:\Users\zaki\src\share>

Source encoding is Shift-JIS

Java

    private static void sprit_example() {
        String string = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama";
        String[] item = string.split(",\\s*");
        for (String i : item) {
            System.out.println(i);
        }
    }

Execution result

Motomachi-Chukagai
Nihon Odori
Bashamichi
Minatomirai
Yokohama

Kotlin

fun split_join() {
    val string: String = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama"
    val item = string.split(",")
    for (i in item) {
        println(i)
    }
}

Execution result

Motomachi-Chukagai
Nihon Odori
Bashamichi
Minatomirai
Yokohama

Unlike Java, regular expressions cannot be used as they are, and you need to create a Regex instance.

fun split_regexp() {
    val string: String = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama"
    val item = string.split(Regex(",\\s*"))
    for (i in item) {
        println(i)
    }
}
Motomachi-Chukagai
Nihon Odori
Bashamichi
Minatomirai
Yokohama

Python2

#!/usr/bin/python2
# -*- coding: utf-8 -*-

string = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama"
item = string.split(", ")

for i in item:
    print i

The output is as follows

Motomachi-Chukagai
Nihon Odori
Bashamichi
Minatomirai
Yokohama

You need to import re to use regular expressions.

import re
item = re.split(r',\s*', string)
for i in item:
    print i

Python3

Similar to Python2.

#!/usr/bin/python3

string = "Motomachi-Chukagai,Nihon Odori,Bashamichi,Minatomirai,Yokohama"
item = string.split(", ")

for i in item:
    print(i)


import re
item = re.split(r',\s*', string)
for i in item:
    print(i)

join

For each language, declare an ʻitem array equivalent to the one listed in splitabove, combine with", ", add " before and after", and "quote" A code that separates the strings from each other with commas. (Addition of " `before and after is processed by simple string concatenation)

Perl

my @item = ("Motomachi-Chukagai", "Nihon Odori", "Bashamichi", "Minatomirai", "Yokohama");
my $join = '"' . (join '","', @item) . '"';
print $join, "\n";

Execution result

"Motomachi-Chukagai","Nihon Odori","Bashamichi","Minatomirai","Yokohama"

PowerShell

$item = @("Motomachi-Chukagai", "Nihon Odori", "Bashamichi", "Minatomirai", "Yokohama")
$join = '"' + ($item -join '","') + '"'
Write-Output $join

Execution result

"Motomachi-Chukagai","Nihon Odori","Bashamichi","Minatomirai","Yokohama"

Java

String item[] = {"Motomachi-Chukagai", "Nihon Odori", "Bashamichi", "Minatomirai", "Yokohama"};
String join = "\"" + String.join("\",\"", item) + "\"";
System.out.println(join);

Execution result

"Motomachi-Chukagai","Nihon Odori","Bashamichi","Minatomirai","Yokohama"

Kotlin

val item = arrayOf("Motomachi-Chukagai", "Nihon Odori", "Bashamichi", "Minatomirai", "Yokohama")
val join = '"' + item.joinToString(separator = "\",\"") + '"'
println(join)

Execution result

"Motomachi-Chukagai","Nihon Odori","Bashamichi","Minatomirai","Yokohama"

Python2

item = ["Motomachi-Chukagai", "Nihon Odori", "Bashamichi", "Minatomirai", "Yokohama"]
join = '"' + '","'.join(item) + '"'
print join
"Motomachi-Chukagai","Nihon Odori","Bashamichi","Minatomirai","Yokohama"

Python3

This is also the same as Python 2

item = ["Motomachi-Chukagai", "Nihon Odori", "Bashamichi", "Minatomirai", "Yokohama"]
join = '"' + '","'.join(item) + '"'
print(join)

Execution result

"Motomachi-Chukagai","Nihon Odori","Bashamichi","Minatomirai","Yokohama"

Recommended Posts

List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
MessagePack-Try to link Java and Python with RPC
Convert strings to character-by-character list format with python
Benchmark for C, Java and Python with prime factorization
Python and Ruby split
Investigate Java and python data exchange with Apache Arrow
Initialize list with python
screen and split screen with python and ssh login to remote server
[Python] Summary of how to use split and join functions
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Programming with Python and Tkinter
Encryption and decryption with Python
3-3, Python strings and character codes
Python and hardware-Using RS232C with Python-
I compared Java and Python!
Python memo using perl --join
Python list and tuples and commas
Python list comprehensions and generators
python with pyenv and venv
Works with Python and R
Extract bigquery dataset and table list with python and output as CSV
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Communicate with FX-5204PS with Python and PyUSB
Difference between Ruby and Python split
Shining life with Python and OpenCV
Difference between java and python (memo)
Install Python 2.7.9 and Python 3.4.x with pip.
Difference between list () and [] in Python
perl objects and python class part 2.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python and Beautiful Soup
Convert list to DataFrame with python
JSON encoding and decoding with python
[Python] Join two tables with pandas
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
[Beginner] Extract character strings with Python
I played with PyQt5 and Python3
perl objects and python class part 1.
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Challenge to create time axis list report with Toggl API and Python