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