LoginSignup
13
9

More than 5 years have passed since last update.

文字列操作比較17言語[JavaScript/TypeScript/CoffeeScript/C#/VB.net/VBS/C++/D言語/R言語/Ruby/Python/Lua/HSP/CommonLisp/Scheme/EmacsLisp/Clojure]

Last updated at Posted at 2016-11-30

簡単な文字列処理をする時も言語変えるといつも忘れてしまうので
比較的良く使う言語をベースに文字列操作(と少しの配列処理)の例題と構文の比較表を作ってみた。
今回は以下の17言語についてのものとする。

JavaScript、TypeScript、CoffeeScript、
C#、VisualBasic.NET、VBScript、C++、D言語、R言語、
Ruby、Python、Lua、HotSoupProcessor、
CommonLisp、Scheme、EmacsLisp、Clojure

ちょいちょい使わない言語も入っているけど比較するのに良い機会だと思って選択。
Lisp4つもは正直やるつもりなかったけれども。
AltJSは構文変わらないけどおまけ。

文字列操作例題

初めの文字列"2\$4\$8\$16"から以下のような文字列を出力するものとする。

出力
2$4$8$16
2#4#8#16
4
5
62
2++4++8++16++32
Hoge->2++4++8++16++32
Hoge->2++4
->2++4

これはそれぞれ次のような処理を行った結果である。
それを各言語にて行う。

出力
"2$4$8$16""$""#"に置換
    ↓
"2#4#8#16""#"を区切り文字に配列へ分割
その要素数を表示
    ↓
"4"
    ↓
配列に要素"32"を追加
その要素数を表示
    ↓
"5"
    ↓
配列を整数型に変換し要素の合計値を算出
    ↓
"62"
    ↓
整数変換前の配列に"++"を区切り文字とし、文字列として結合
    ↓
"2++4++8++16++32"
    ↓
文字列"Hoge->"と上記文字列を連結
    ↓
"Hoge->2++4++8++16++32"
    ↓
左から10文字分切り出す
    ↓
"Hoge->2++4"
    ↓
右から6文字分切り出す
    ↓
"->2++4"

JavaScript

node v6.9.1

起動コマンド
C:\> node strTest.js
JavaScript
"use strict";   //strinc mode

function strTest(){
    //文字列定義
    var strText="2$4$8$16";
    console.log(strText);

    //文字列置換
    strText=strText.replace(/\$/g,"#");
    console.log(strText);

    //配列分割
    var strList=strText.split(/#/);
    console.log(strList.length);

    //配列要素追加
    strList.push("32");
    //strList=strList.concat("32");
    //[].push.apply(strList,["32"]);
    console.log(strList.length);

    //map処理
    var intList=strList.map(val=>parseInt(val,10));
    //var intList=strList.map(Number);

    //足し合わせ
    var sum=intList.reduce((xsum,val)=>xsum+val);
    console.log(sum);

    //配列結合
    strText=strList.join("++");
    console.log(strText);

    //文字列連結
    strText="Hoge->"+strText;
    console.log(strText);

    //文字列切り出し左
    strText=strText.slice(0,10);
    //strText=strText.substr(0,10);
    //strText=strText.substring(0,10);
    console.log(strText);

    //文字列切り出し右
    strText=strText.slice(-6);
    //strText=strText.substr(-6);
    //strText=strText.substring(strText.length-6);
    console.log(strText);

    //コンソール停止
    process.stdin.once("data",process.exit);
}

strTest();

TypeScript

tsc Version 1.8.10

起動コマンド
C:\> tsc strTest.ts & node strTest.js
TypeScript
function strTest(){
    //文字列定義
    var strText:string="2$4$8$16";
    console.log(strText);

    //文字列置換
    strText=strText.replace(/\$/g,"#");
    console.log(strText);

    //配列分割
    var strList:string[]=strText.split(/#/);
    console.log(strList.length);

    //配列要素追加
    strList.push("32");
    //strList=strList.concat("32");
    //[].push.apply(strList,["32"]);
    console.log(strList.length);

    //map処理
    var intList:number[]=strList.map(val=>parseInt(val,10));
    //var intList:number[]=strList.map(Number);

    //足し合わせ
    var sum:number=intList.reduce((xsum,val)=>xsum+val);
    console.log(sum);

    //配列結合
    strText=strList.join("++");
    console.log(strText);

    //文字列連結
    strText="Hoge->"+strText;
    console.log(strText);

    //文字列切り出し左
    strText=strText.slice(0,10);
    //strText=strText.substr(0,10);
    //strText=strText.substring(0,10);
    console.log(strText);

    //文字列切り出し右
    strText=strText.slice(-6);
    //strText=strText.substr(-6);
    //strText=strText.substring(strText.length-6);
    console.log(strText);

    //コンソール停止
    process.stdin.once("data",process.exit);
}

strTest();

CoffeeScript

CoffeeScript version 1.8.0

起動コマンド
C:\> coffee strTest.coffee
CoffeeScript
strTest=->
    #文字列定義
    strText="2$4$8$16"
    console.log strText

    #文字列置換
    strText=strText.replace /\$/g,"#"
    console.log strText

    #配列分割
    strList=strText.split /#/
    console.log strList.length

    #配列要素追加
    strList.push "32"
    #[].push.apply strList,["32"]
    #strList=strList.concat "32"
    console.log strList.length

    #map処理
    intList=strList.map (val)->parseInt val,10
    #intList=strList.map Number

    #足し合わせ
    sum=intList.reduce (xsum,val)->xsum+val
    console.log sum

    #配列結合
    strText=strList.join "++"
    console.log strText

    #文字列連結
    strText="Hoge->"+strText
    console.log strText

    #文字列切り出し左
    strText=strText.slice 0,10
    #strText=strText.substr 0,10
    #strText=strText.substring 0,10
    console.log strText

    #文字列切り出し右
    strText=strText.slice -6
    #strText=strText.substr -6
    #strText=strText.substring strText.length-6
    console.log strText

    #コンソール停止
    process.stdin.once "data",process.exit

strTest()

VB.Net

Microsoft (R) Visual Basic Compiler バージョン 1.3.1.60616

起動コマンド
C:\> vbc /nologo /optimize+ strTest.vb & strTest.exe
VB.net
Option Strict On    'strict mode

Imports System.Console                  'WriteLine, ReadLine
Imports System.Collections.Generic      'List
Imports System.Linq                     'Select, Aggregate
Imports System.Text.RegularExpressions  'Regex

Module Program
    Sub strTest()
        '文字列定義
        Dim strText As String="2$4$8$16"
        WriteLine(strText)

        '文字列置換
        strText=strText.Replace("$","#")
        'strText=Regex.Replace(strText,"\$","#")
        WriteLine(strText)

        '配列(リスト)分割
        Dim strList As New List(Of String)(Split(strText,"#"))
        'Dim strList As New List(Of String)(strText.Split("#"))
        'Dim strList As New List(Of String)(strText.Split({"#"},StringSplitOptions.None))
        'Dim strList As New List(Of String)(New Regex("\#").Split(strText))
        WriteLine(strList.Count)

        'リスト要素追加
        strList.Add("32")
        'strList.AddRange({"32"})
        WriteLine(strList.Count)

        'map処理
        Dim intList As New List(Of Integer)(strList.ConvertAll(Function(val)CInt(val)))
        'Dim intList As New List(Of Integer)(strList.Select(Function(val)CInt(val)))
        'Dim intList As New List(Of Integer)(strList.ConvertAll(AddressOf Integer.Parse))
        'Dim intList As New List(Of Integer)(strList.Select(AddressOf Integer.Parse))

        '足し合わせ
        Dim sum As Integer=intList.Aggregate(Function(xsum,val)xsum+val)
        WriteLine(sum)

        'リスト結合
        strText=String.Join("++",strList)
        WriteLine(strText)

        '文字列連結
        strText="Hoge->" & strText
        WriteLine(strText)

        '文字列切り出し左
        strText=Left(strText,10)
        'strText=Mid(strText,1,10)
        'strText=strText.Substring(0,10)
        WriteLine(strText)

        '文字列切り出し右
        strText=Right(strText,6)
        'strText=Mid(strText,strText.Length-6+1)
        'strText=strText.Substring(strText.Length-6)
        WriteLine(strText)

        'コンソール停止
        ReadLine()
    End Sub

    Sub Main()
        strTest()
    End Sub
End Module

VBScript

Microsoft (R) Windows Script Host Version 5.8

起動コマンド
C:\> cscript //nologo //E:VBScript strTest.vbs
WSH_VBScript
Sub strTest
    '文字列定義
    Dim strText
    strText="2$4$8$16"
    WScript.Echo strText

    '文字列置換
    strText=Replace(strText,"$","#")
    WScript.Echo strText

    '配列分割
    Dim strList
    strList=Split(strText,"#")
    WScript.Echo UBound(strList)+1

    '配列要素追加
    ReDim Preserve strList(UBound(strList)+1)
    strList(UBound(strList))="32"
    WScript.Echo UBound(strList)+1

    'map処理
    ReDim intList(UBound(strList))
    Dim i
    For i=0 To UBound(intList)
        intList(i)=CInt(strList(i))
    Next

    '足し合わせ
    Dim sum
    sum=0
    For Each val In intList
        sum=sum+val
    Next
    WScript.Echo sum 

    'リスト結合
    strText=Join(strList,"++")
    WScript.Echo strText 

    '文字列連結
    strText="Hoge->" & strText
    'strText="Hoge->"+strText
    WScript.Echo strText 

    '文字列切り出し左
    strText=Left(strText,10)
    'strText=Mid(strText,1,10)
    WScript.Echo strText

    '文字列切り出し右
    strText=Right(strText,6)
    'strText=Mid(strText,Len(strText)-6+1)
    WScript.Echo strText

    'コンソール停止
    WScript.StdIn.ReadLine()
End Sub

strTest

C#

Microsoft (R) Visual C# Compiler バージョン 1.3.1.60616

起動コマンド
C:\> csc /nologo /optimize+ strTest.cs & strTest.exe
C#
using System;                           //Console
using System.Collections.Generic;       //List
using System.Linq;                      //Select, Aggregate
using System.Text.RegularExpressions;   //Regex

class Program{
    static void strTest(){
        //文字列定義
        string strText="2$4$8$16";
        Console.WriteLine(strText);

        //文字列置換
        strText=strText.Replace("$","#");
        //strText=Regex.Replace(strText,"\\$","#");
        Console.WriteLine(strText);

        //配列(リスト)分解
        List<String> strList=new List<string>(strText.Split('#'));
        //List<String> strList=new List<string>(strText.Split(new string[]{"#"},StringSplitOptions.None));
        //List<String> strList=new List<string>(new Regex("\\#").Split(strText));
        Console.WriteLine(strList.Count);

        //配列(リスト)要素追加
        strList.Add("32");
        //strList.AddRange(new string[]{"32"});
        Console.WriteLine(strList.Count);

        //map
        //List<int> intList=new List<int>(strList.ConvertAll(val=>int.Parse(val)));
        //List<int> intList=new List<int>(strList.Select(val=>int.Parse(val)));
        //List<int> intList=new List<int>(strList.ConvertAll(int.Parse));
        List<int> intList=new List<int>(strList.Select(int.Parse));

        //足し合わせ
        int sum=intList.Aggregate((xsum,val)=>xsum+val);
        Console.WriteLine(sum);

        //配列(リスト)結合
        strText=string.Join("++",strList);
        Console.WriteLine(strText);

        //文字列連結
        strText="Hoge->"+strText;
        Console.WriteLine(strText);

        //文字列切り出し左
        strText=strText.Substring(0,10);
        Console.WriteLine(strText);

        //文字列切り出し右
        strText=strText.Substring(strText.Length-6);
        Console.WriteLine(strText);

        //コンソール停止
        Console.ReadLine();
    }

    static void Main(){
        strTest();
    }
}

C++

g++ (GCC) 4.9.3

起動コマンド
C:\> g++ -std=c++11 strTest.cpp & strTest.exe
C++
#include <stdio.h>      //getchar
#include <stdlib.h>     //atoi
#include <iostream>     //cout<<
#include <string>       //string, c_str
#include <vector>       //vector
#include <algorithm>    //replace
#include <sstream>      //stringstream
using namespace std;    //std:: 名前空間の記述省略

void strTest(){
    //文字列定義
    string strText="2$4$8$16";
    cout<<strText<<endl;

    //文字列置換
    replace(strText.begin(),strText.end(),'$','#');
    //文字列の置換は不可(char型のみ)
    cout<<strText<<endl;

    //配列分割
    vector<string> strList;
    stringstream ss(strText);
    string val;
    while(getline(ss,val,'#')){
        strList.push_back(val);
    }
    cout<<strList.size()<<endl;

    //配列要素追加
    strList.push_back("32");
    cout<<strList.size()<<endl;

    //map処理
    vector<int> intList;
    for(string val:strList){
        intList.push_back(stoi(val));
        //intList.push_back(atoi(val.c_str()));
    }

    //足し合わせ
    int sum=0;
    for(int val:intList){
        sum+=val;
    }
    cout<<sum<<endl;

    //配列結合
    strText="";
    for(string val:strList){
        if(strText!=""){strText+="++";}
        strText+=val;
    }
    cout<<strText<<endl;

    //文字列連結
    strText="hoge<-"+strText;
    cout<<strText<<endl;

    //文字列切り取り左
    strText=strText.substr(0,10);
    cout<<strText<<endl;

    //文字列切り取り右
    strText=strText.substr(strText.size()-6);
    cout<<strText<<endl;

    //コンソール停止
    getchar();
}

int main(){
    strTest();
    return 0;
}

D言語

DMD32 D Compiler v2.066.1

起動コマンド
C:\> dmd strTest.d & strTest.exe
D言語
import std.stdio;       //writeln, readln
import std.string;      //replace, split
import std.array;       //array
import std.regex;       //regex
import std.conv;        //to!
import std.algorithm;   //map!, reduce!

void strTest(){
    //文字列定義
    string strText="2$4$8$16";
    writeln(strText);

    //文字列置換
    strText=replace(strText,"$","#");
    //strText=replace(strText,regex(r"\$","g"),"#");
    writeln(strText);

    //配列分割
    string[] strList=split(strText,"#");
    //string[] strList=split(strText,regex(r"\#","g"));
    //string[] strList=array(splitter(strText,"#"));
    //string[] strList=array(splitter(strText,regex(r"\#","g")));
    writeln(strList.length);

    //配列要素追加
    strList~="32";
    writeln(strList.length);

    //map処理
    int[] intList=array(map!("to!int(a)")(strList));
    //int[] intList=array(map!(val=>to!int(val))(strList));
    //int[] intList=array(map!(to!int)(strList));

    //足し合わせ
    int sum=reduce!("a+b")(intList);
    //int sum=reduce!((xsum,val)=>xsum+val)(intList);
    writeln(sum);

    //配列結合
    strText=strList.join("++");
    writeln(strText);

    //文字列連結
    strText="Hoge->"~strText;
    writeln(strText);

    //文字列切り出し左
    strText=strText[0..10];
    writeln(strText);

    //文字列切り出し右
    strText=strText[$-6..$];
    writeln(strText);

    //コンソール停止
    readln();
}

void main(){
    strTest();
}

HSP

HSP : Hot Soup Processor ver3.4

起動コマンド
::エディタからコンパイル
C:\> C:\hsp34\hsed3
HSP
#runtime "hsp3cl"   ;コンソール
#cmpopt varinit 1   ;strict mode

#module
    #deffunc strTest
        ;文字列定義
        strText="2$4$8$16"
        mes strText

        ;文字列置換
        strrep strText,"$","#"
        mes strText

        ;配列分割
        sdim strList
        split strText,"#",strList
        mes length(strList)

        ;配列要素追加
        strList.length(strList)="32"
        mes length(strList)

        ;map処理
        dim intList,length(strList)
        #define map(%1,%2,%3,%4) foreach %1:%3=%1(cnt):%2(cnt)=%4:loop
        map strList,intList,val,int(val)

        ;足し合わせ
        #define reduce(%1,%2,%3,%4) %2=%1(0):repeat length(%1)-1:%3=%1(cnt+1):%2=%4:loop
        reduce intList,sum,val,sum+val
        mes sum

        ;配列結合
        strText=""
        foreach strList
            if cnt!=0: strText+="++"
            strText+=strList(cnt)
        loop
        mes strText

        ;文字列連結
        strText="Hoge->"+strText
        mes strText

        ;文字列切り出し左
        strText=strmid(strText,0,10)
        mes strText

        ;文字列切り出し右
        strText=strmid(strText,-1,6)
        mes strText

        ;コンソール停止
        dim exit:input exit,,1
    return
#global

strTest

Ruby

ruby 2.0.0p481 (2014-05-08) [i386-mingw32]

起動コマンド
C:\> ruby strTest.rb
Ruby
def strTest
    #文字列定義
    strText="2$4$8$16"
    puts strText

    #文字列置換
    strText=strText.gsub /\$/,"#"
    puts strText

    #配列分割
    strList=strText.split /#/
    puts strList.length

    #配列要素追加
    strList.push "32"
    #strList=strList.clone.push "32"
    #strList << "32"
    #strList.concat ["32"]
    #strList=strList.dup.concat ["32"]
    #strList+=["32"]
    puts strList.length

    #map処理
    intList=strList.map{|val| val.to_i}
    #intList=strList.collect{|val| val.to_i}

    #足し合わせ
    sum=intList.reduce{|xsum,val| xsum+val}
    #sum=intList.inject{|xsum,val| xsum+val}
    puts sum

    #配列結合
    strText=strList.join "++"
    puts strText

    #文字列連結
    strText="Hoge->" << strText
    #strText="Hoge->" + strText
    puts strText

    #文字列切り出し左
    strText=strText[0,10]
    #strText=strText[0..10-1]
    #strText=strText.slice 0,10
    #strText=strText.slice 0..10-1
    puts strText

    #文字列切り出し右
    strText=strText[-6..-1]
    #strText=strText[strText.length-6,strText.length]
    #strText=strText.slice -6..-1
    #strText=strText.slice strText.length-6,strText.length
    puts strText

    #コンソール停止
    STDIN.gets
end

strTest

Python

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32

起動コマンド
C:\> py -3 strTest.py
Python
import functools    #reduce

def strTest():
    #文字列定義
    strText="2$4$8$16"
    print(strText)

    #文字列置換
    strText=strText.replace("$","#")
    print(strText)

    #配列分割
    strList=strText.split("#")
    print(len(strList))

    #配列要素追加
    strList.append("32")
    print(len(strList))

    #map処理
    intList=map(lambda val:int(val),strList)
    #intList=map(int,strList)

    #足し合わせ
    sum=functools.reduce(lambda xsum,val:xsum+val,intList)
    print(sum)

    #配列結合
    strText="++".join(strList)
    print(strText)

    #文字列連結
    strText="Hoge->"+strText
    print(strText)

    #文字列切り出し左
    strText=strText[0:10]
    print(strText)

    #文字列切り出し右
    strText=strText[-6:]
    print(strText)

    #コンソール停止
    input()

strTest()

Lua

Lua 5.3.3

起動コマンド
C:\> lua strTest.lua
Lua
function strTest()
    --文字列定義
    local strText="2$4$8$16"
    print(strText)

    --文字列置換
    strText=string.gsub(strText,"%$","#")
    print(strText)

    --配列分割
    --Luaにsplitは存在しない
    local strList=((function(data,sepa)
        local result={}
        for chank in string.gmatch(data..sepa,"(.-)"..sepa) do
            table.insert(result,chank)
        end
        return result
    end)(strText,"#"))
    print(#strList)

    --配列要素追加
    table.insert(strList,"32")
    print(#strList)

    --map処理
    local intList={}
    for i,val in ipairs(strList) do
        intList[i]=tonumber(val)
    end

    --足し合わせ
    sum=0
    for i,val in ipairs(intList) do
        sum=sum+val
    end
    print(sum)

    --配列結合
    strText=""
    for i,val in ipairs(strList) do
        if strText~="" then
            strText=strText.."++"
        end
        strText=strText..val
    end
    print(strText)

    --文字列連結
    strText="Hoge->"..strText
    print(strText)

    --文字列切り出し左
    strText=string.sub(strText,1,10)
    print(strText)

    --文字列切り出し右
    strText=string.sub(strText,-6)
    print(strText)

    --コンソール停止
    io.read()
end

strTest()

R言語

R scripting front-end version 3.3.0 (2016-05-03)

起動コマンド
C:\> rscript strTest.r
R言語
strTest<-function(){
    #文字列定義
    strText<-"2$4$8$16"
    cat(strText,"\n")

    #文字列置換
    strText<-gsub("\\$","#",strText)
    cat(strText,"\n")

    #配列分割
    strList<-unlist(strsplit(strText,"#"))
    cat(length(strList),"\n")

    #配列要素追加
    strList<-c(strList,"32")
    cat(length(strList),"\n")

    #map処理
    intList<-Map(function(val){as.integer(val)},strList)
    #intList<-(function(val){as.integer(val)})(strList)
    #intList<-Map(as.integer,strList)

    #足し合わせ
    sum<-Reduce(function(xsum,val){xsum+val},intList)
    cat(sum,"\n")

    #配列結合
    strText<-""
    for(val in strList){
        if(strText!=""){
            strText<-paste(strText,"++",sep="")
        }
        strText<-paste(strText,val,sep="")
    }
    cat(strText,"\n")

    #文字列連結
    strText<-sprintf("Hoge->%s",strText)
    cat(strText,"\n")

    #文字列切り出し左
    strText<-substr(strText,1,10)
    cat(strText,"\n")

    #文字列切り出し右
    strText<-substr(strText,nchar(strText)-6+1,nchar(strText))
    cat(strText,"\n")

    #コンソール停止
    readLines(file("stdin"), n <- 1L)
}

strTest()

CommonLisp

SBCL 1.3.5

起動コマンド
C:\> sbcl --script strTest.lisp
CommonLisp
(defun strTest()
    (let (
        ;文字列定義(束縛)
        (strText "2$4$8$16")
        strList intList
        (sum 0)
    )
        (format t "~A~%" strText)

        ;文字列置換
        (setf strText (substitute #\# #\$ strText))
        ;文字列の置換は不可(char型のみ)
        (format t "~A~%" strText)

        ;配列(リスト)分割
        ;CommonLispにsplitは存在しない
        (setf strList ((lambda (data sepa)
            (let ((result '()) (now1 0) now2)
                (loop
                    (setf now2 (search sepa data :start2 now1))
                    (setf result (append result `(,(subseq data now1 now2))))
                    (if (not now2)
                        (return result)
                        (setf now1 (+ now2 (length sepa)))
                    )
                )
            )
        ) strText "#"))
        (format t "~d~%" (length strList))

        ;配列(リスト)要素追加
        (setf strList (append strList '("32")))
        (format t "~d~%" (length strList))

        ;map処理
        (setf intList (mapcar #'(lambda(val)(parse-integer val)) strList))
        ;(setf intList (mapcar 'parse-integer strList))

        ;足し合わせ
        (setf sum (apply '+ intList))
        ;(setf sum (reduce '+ intList))
        (format t "~d~%" sum)

        ;配列(リスト)結合
        (setf strText (format nil "~{~A~^++~}" strList))
        (format t "~A~%" strText)

        ;文字列連結
        (setf strText (format nil "Hoge->~A" strText))
        ;(concatenate 'string "Hoge->" strText)
        (format t "~A~%" strText)

        ;文字列切り出し左
        (setf strText (subseq strText 0 10))
        (format t "~A~%" strText)

        ;文字列切り出し右
        (setf strText (subseq strText (- (length strText) 6)))
        (format t "~A~%" strText)

        ;コンソール停止
        (read-line)
    )
)

(strTest)

EmacsLisp

GNU Emacs 24.5.1 (i686-pc-mingw32)

起動コマンド
C:\> emacs -Q --script strTest.el
EmacsLisp
(defun strTest()
    (let (
        ;文字列定義(束縛)
        (strText "2$4$8$16")
        strList intList
        (sum 0)
    )
        (princ (format "%s\n" strText))

        ;文字列置換
        (setf strText (replace-regexp-in-string "\\$" "#" strText))
        (princ (format "%s\n" strText))

        ;配列(リスト)分割
        (setf strList (split-string strText "#"))
        (princ (format "%d\n" (length strList)))

        ;配列(リスト)要素追加
        (setf strList (append strList '("32")))
        (princ (format "%d\n" (length strList)))

        ;map処理
        (setf intList (mapcar #'(lambda(val)(string-to-number val)) strList))
        ;(setf intList (mapcar 'string-to-number strList))

        ;足し合わせ
        (setf sum (apply '+ intList))
        (princ (format "%d\n" sum))

        ;配列(リスト)結合
        (setf strText (mapconcat 'identity strList "++"))
        (princ (format "%s\n" strText))

        ;文字列連結
        (setf strText (concat "Hoge->" strText))
        (princ (format "%s\n" strText))

        ;文字列切り出し左
        (setf strText (substring strText 0 10))
        (princ (format "%s\n" strText))

        ;文字列切り出し右
        (setf strText (substring strText -6))
        (princ (format "%s\n" strText))

        ;コンソール停止
        (read-string "")
    )
)

(strTest)

Scheme

Gauche scheme shell, version 0.9.4 [utf-8,wthreads], i686-pc-mingw32

起動コマンド
C:\> gosh strTest.scm
Scheme
(define (strTest)
    (let (
        ;文字列定義(束縛)
        (strText "2$4$8$16")
        (strList '())
        (intList '())
        (sum 0)
    )
        (format #t "~A\n" strText)

        ;文字列置換
        (set! strText (regexp-replace-all #/\$/ strText "#"))
        (format #t "~A\n" strText)

        ;配列(リスト)分割
        (set! strList (string-split strText "#"))
        (format #t "~d\n" (length strList))

        ;配列(リスト)要素追加
        (set! strList (append strList '("32")))
        (format #t "~d\n" (length strList))

        ;map処理
        (set! intList (map (lambda (val) (string->number val)) strList))
        ;(set! intList (map string->number strList))

        ;足し合わせ
        (set! sum (apply + intList))
        ;(set! sum (fold + 0 intList))
        (format #t "~d\n" sum)

        ;配列(リスト)結合
        (set! strText (string-join strList "++"))
        (format #t "~A\n" strText)

        ;文字列連結
        (set! strText (string-append "Hoge->" strText))
        (format #t "~A\n" strText)

        ;文字列切り出し左
        (set! strText (substring strText 0 10))
        (format #t "~A\n" strText)

        ;文字列切り出し右
        (set! strText (substring strText (- (string-length strText) 6) -1))
        (format #t "~A\n" strText)

        ;コンソール停止
        (read-line)
    )
)

(strTest)

Clojure

Clojure 1.8.0 (CLR)

起動コマンド
C:\> Clojure.Main strTest.clj
clojure
(require '[clojure.string :as string])

(defn strTest[]
    ;文字列定義(束縛)
    (let [strText "2$4$8$16"]
        (println strText)

        ;文字列置換
        (let [strText (string/replace strText "$" "#")]
            (println strText)

            ;配列(リスト)分割
            (let [strList (string/split strText #"\#")]
                (println (count strList))

                ;配列(リスト)要素追加
                (let [strList (conj strList "32")]
                    (println (count strList))

                    ;map処理
                    (let [intList (map (fn [val] (int val)) strList)]
                    ;(let [intList (map int strList)]

                        ;足し合わせ
                        (let [sum (apply + intList)]
                            (println sum)
                        )
                    )

                    ;配列(リスト)結合
                    (let [strText (string/join "++" strList)]
                        (println strText)

                        ;文字列連結
                        (let [strText (str "Hoge->" strText)]
                            (println strText)

                            ;文字列切り出し左
                            (let [strText (subs strText 0 10)]
                                (println strText)

                                ;文字列切り出し右
                                (let [strText (subs strText (- (count strText) 6))]
                                    (println strText)
                                )
                            )
                        )
                    )
                )
            )
        )
    )

    ;コンソール停止
    (read-line)
)
(strTest)

まとめ

言語 文字列置換 文字列分割 文字列連結 配列結合
JavaScript
TypeScript
CoffeeScript
replace split +演算子 join
VB.Net Replace Split &演算子
+演算子
Join
VBScript Replace Split &演算子
+演算子
Join
C# Replace Split +演算子 Join
C++ replace
※char型のみ
無し +演算子 無し
D言語 replace split
splitter
~演算子 join
HSP strrep ※破壊 split +演算子 無し
Ruby gsub split <<演算子
+演算子
join
Python replace split +演算子 join
Lua string.gsub 無し ..演算子 無し
R言語 gsub strsplit paste
sprintf 等
paste
CommonLisp substitute
※char型のみ
無し format nil
concatenate 'string 等
format nil
EmacsLisp replace-regexp-in-string split-string concat mapconcat 'identity
Scheme regexp-replace-all
※Gauche拡張
string-split
※Gauche拡張
string-append string-join
※Gauche拡張
Clojure string/replace string/split str string/join
言語 文字列切り出し 配列要素追加 map関数 reduce関数
JavaScript
TypeScript
CoffeeScript
slice
substr
substring
push ※破壊
concat
push.apply ※破壊
map reduce
VB.Net Left
Right
Mid
Substring
Add ※破壊
AddRange ※破壊
ConvertAll
Select
Aggregate
VBScript Left
Right
Mid
無し 無し 無し
C# Substring Add ※破壊
AddRange ※破壊
ConvertAll
Select
Aggregate
C++ substr push_back 無し 無し
D言語 [a..b]
[\$a..b\$]
~演算子 map! reduce!
HSP strmid a(length(a))=b
a.length(a)=b
無し 無し
Ruby [a,b]
[a..b]
slice
push ※破壊
<<演算子 ※破壊
concat ※破壊
+演算子
map
collect
reduce
inject
Python [a:b] append ※破壊 map functools.reduce
Lua string.sub table.insert ※破壊 無し 無し
R言語 substr c ※破壊 Map Reduce
CommonLisp subseq append mapcar
map 'list
reduce
EmacsLisp substring append mapcar 無し
Scheme substring append map
map-to
fold
Clojure subs conj map reduce

※1 .NET言語の配列はList<T>型であることとする。
※2 C++の配列はvector<T>型であることとする。
※3 HSPの配列自動拡張は行うたびに新しい配列を作り直すため、多用するとパフォーマンスが低下する。
※4 Rubyの破壊メソッドはhoge.clone.pushやhoge.dub.pushのようにcloneかdubをチェーンで繋げることで非破壊メソッドとすることができる。
※5 Lispで使用しているapplyはリストを引数に分割して任意の関数に渡せる関数。reduceとapplyは本来の用途が異なる。

あとがき

まとめてみて思ったけど、構文名以外にも使い方も結構変わってくるのが辛いと思った。
具体的には関数チェーンの有無、引数の順序、正規表現あたり。
特に\$の正規表現は何種類かあってハマりかけたものもあった。

あとはCommonLispの文字列処理がもう少し優秀であってくれさえすれば、
恐らくLisp4つもやることにはならなかった。
EmacsLispの文字列操作は結構優秀だ。
Schemeは処理系依存多すぎなのでなんとも言えない。

  • 追記
    コメント頂いて、mapに渡すコールバック関数をラムダ形式と関数名指定で併記することにしました。
    それ以外にも、同じ動作の処理を行える構文はコメントで埋め込んでいます。
    (Rubyがものすごい増えた…)
    • C++追加しました。
      何をするにも無い無い尽くしでかなりしんどかった…
      C++には文字列操作をさせるべきでない。
13
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9