8
5

More than 5 years have passed since last update.

SwiftでもPythonみたいに文字列操作したい!

Last updated at Posted at 2019-08-20

最近使い始めたSwiftでもPythonみたいに文字列操作したい!

SwiftでもPythonみたいに文字列のスライスしたいなぁ

ってことでStringを拡張してPythonの文字列操作メソッドを生やすライブラリを作ってみました。

SwiftyPyString

https://github.com/ChanTsune/SwiftyPyString

Swift5で動きます。
Unicode準拠 & Pythonのドキュメント準拠で作ってます。

インストール

CocoaPods、Carthage、SwiftPMの主要なパッケージマネージャ3つに対応させています。

CocoaPods

pod 'SwiftyPyString'

Carthage

github 'ChanTsune/SwiftyPyString'

SwiftPM

import PackageDescription

let package = Package(
    name: "YourProject",
    dependencies: [
        .Package(url: "https://github.com/ChanTsune/SwiftyPyString.git", from: "1.0.1")
    ]
)

文字列操作

添字アクセス

let str = "0123456789"
str[0]
// 0
str[-1]
// 9

Python準拠なので負の数を利用した後ろからのアクセスもサポートしています。

文字列のスライス

let str = "0123456789"
str[0,5]
// 01234
str[0,8,2]
// 0246
str[nil,nil,-1]
// 9876543210

やりたかったスライスです。

一度これに慣れると他の言語でも使いたくなるやつです。

(だからこれを作ったわけですが笑)

コロンだけの省略記法はnilで代用することにしました。

ちなみに同じ動作をPythonで書くと以下のようになります。

str = "0123456789"
str[0:5]
# 01234
str[0:8:2]
# 0246
str[::-1]
# 9876543210

文字列検索

// 先頭からの検索  
"123412312312345".find("123") // 0

// 開始位置を指定して検索
"123412312312345".find("123",start:2) // 4

// 終了位置を指定して検索
"123412312312345".find("123",end:1) // -1

// 末尾からの検索
"123412312312345".rfind("123") // 10

末尾からの検索も同様に開始位置と終了位置を指定して検索できます。

文字列結合

let array = ["abc","def","ghi"]
"".join(array) // "abcdefghi"
"-".join(array) // "abc-def-ghi"
"++".join(array) // "abc++def++ghi"

トリミング

// 右端のみ
"rstrip sample   ".rstrip() // "rstrip sample"
"rstrip sample   ".rstrip("sample ") // "rstri"
"  rstrip sample".rstrip() // "  rstrip sample"

// 左端のみ
"  lstrip sample".lstrip() // "lstrip sample"
"  lstrip sample".lstrip(" ls") // "trip sample"
"lstrip sample".lstrip() // "lstrip sample"

// 両端
"   spacious   ".strip() // "spacious"
"www.example.com".strip("cmowz.") // "example"

文字列分割

行ごとの分割
"abc\nabc".splitlines() // ["abc", "abc"]
"abc\r\nabc\n".splitlines() // ["abc", "abc"]

// 改行文字を残して分割
"abc\nabc\r".splitlines(true) // ["abc\n", "abc\r"]
"abc\r\nabc\n".splitlines(true) // ["abc\r\n", "abc\n"]
指定文字での分割
"a,b,c,d,".split(",") // ["a", "b", "c", "d", ""]

"aabbxxaabbaaddbb".split("aa") // ["", "bbxx", "bb", "ddbb"]

// 分割の回数を指定
"a,b,c,d,".split(",", maxsplit: 2) // ["a", "b", "c,d,"]

出現回数カウント

"abc abc abc".count("abc") // 3

// 開始位置の指定
"abc abc abc".count("abc", start:2) // 2

// 終了位置の指定
"abc abc abc".count("abc", end:1) // 0

ゼロ埋め

"abc".zfill(1) // "abc"
"abc".zfill(5) // "00abc"

// 符号付きの場合
"+12".zfill(5) // "+0012"
"-3".zfill(5) // "-0003"
"+12".zfill(2) // "+12"

符号付きの場合は符号の後ろにゼロが入ります。

さいごに

以上、簡単に主要な機能の説明をさせて頂きました。

紹介したメソッド以外にもPython3.7.3の時点で利用できるstr型のメソッドは言語機能的に実装出来ない、あるいは実装が難しいもの以外はほとんど実装してあります。

実装してあるメソッドの一覧は、こちらをご覧ください。
https://github.com/ChanTsune/SwiftyPyString/blob/master/README.md

一部、標準のメソッドと機能がかぶるものもありますが、PythonからSwiftに移植したいなんて言う事があれば多少は移植作業が楽になるのではないでしょうか?(普通にPythonを動くようにした方が多分楽 笑)

そうでなくともPythonからプログラミングを始めたという人なら、慣れ親しんだPythonの文字列操作ができるようになるので比較的便利ではないでしょうか?

このメソッド実装できるよ、とかこっちの実装の方がパフォーマンスいいんじゃない? Swiftだったらこう書くと綺麗だよ等ありましたら教えてください。

プルリクお待ちしております。

もしあれば、バグ報告とかも嬉しいです。

以前、C++版も作っているのでこちらも宜しければ
c++でもpythonのstr型のメソッドを使いたい!
https://qiita.com/ChanTsune/items/38814ca81738877c51fe

8
5
1

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
8
5