{"id":15728858,"url":"https://github.com/chantsune/swiftypystring","last_synced_at":"2026-01-21T17:31:42.276Z","repository":{"id":35062261,"uuid":"190110158","full_name":"ChanTsune/SwiftyPyString","owner":"ChanTsune","description":"Provide python like string operations library for swift","archived":false,"fork":false,"pushed_at":"2023-11-01T12:53:49.000Z","size":1164,"stargazers_count":2,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T06:48:07.924Z","etag":null,"topics":["swift","swift-extensions","swift-library"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ChanTsune.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["ChanTsune"]}},"created_at":"2019-06-04T01:50:59.000Z","updated_at":"2024-09-25T02:30:22.000Z","dependencies_parsed_at":"2024-10-24T21:22:48.763Z","dependency_job_id":"30544af2-07d3-4b24-904f-04cbeecc884c","html_url":"https://github.com/ChanTsune/SwiftyPyString","commit_stats":{"total_commits":181,"total_committers":5,"mean_commits":36.2,"dds":0.56353591160221,"last_synced_commit":"3bc848635530810dd0effeb8743ce31080a10174"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanTsune%2FSwiftyPyString","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanTsune%2FSwiftyPyString/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanTsune%2FSwiftyPyString/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanTsune%2FSwiftyPyString/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ChanTsune","download_url":"https://codeload.github.com/ChanTsune/SwiftyPyString/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615479,"owners_count":20967183,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["swift","swift-extensions","swift-library"],"created_at":"2024-10-03T23:07:01.349Z","updated_at":"2026-01-21T17:31:42.230Z","avatar_url":"https://github.com/ChanTsune.png","language":"Swift","readme":"# SwiftyPyString  \n\n[![Build Status](https://travis-ci.org/ChanTsune/SwiftyPyString.svg?branch=master)](https://travis-ci.org/ChanTsune/SwiftyPyString)\n![Cocoapods](https://img.shields.io/cocoapods/l/SwiftyPyString)\n![carthage](https://img.shields.io/badge/carthage-compatible-brightgreen.svg)\n![GitHub release](https://img.shields.io/github/release/ChanTsune/SwiftyPyString)\n![Cocoapods platforms](https://img.shields.io/cocoapods/p/SwiftyPyString)\n![Swift Version](https://img.shields.io/badge/Swift-5-blue.svg)\n  \nSwiftyPyString is a string extension for Swift.  \nThis library provide Python compliant String operation methods.  \n\n## Installation  \n\n### Cocoapods  \n\n```ruby\npod 'SwiftyPyString'\n```\n\n### Carthage  \n\n```bash\ngithub 'ChanTsune/SwiftyPyString'\n```\n\n### Swift Package Manager  \n\n```swift\nimport PackageDescription\n\nlet package = Package(\n    name: \"YourProject\",\n    dependencies: [\n        .package(url: \"https://github.com/ChanTsune/SwiftyPyString.git\", from: \"\\(version)\")\n    ]\n)\n```\n\n## Usage  \n\n```swift\nimport SwiftyPyString\n```\n\n### String extension\n\n#### String sliceing subscript  \n\n```swift\nlet str = \"0123456789\"\nstr[0]\n// 0\nstr[-1]\n// 9\n```\n\n#### Slice String\n\n```swift\nlet str = \"0123456789\"\n\nstr[0,5]\n// 01234\nstr[0,8,2]\n// 0246\nstr[nil,nil,-1]\n// 9876543210\n```\n\nUse Slice object case  \n\n```swift\nlet str = \"0123456789\"\nvar slice = Slice(start:0, stop:5)\nvar sliceStep = Slice(start:0, stop:8, step:2)\n\nstr[slice]\n// 01234\nstr[sliceStep]\n// 0246\n```\n\n#### String Multiplication  \n\n```swift\nvar s = \"Hello World! \" * 2\n\n// \"Hello World! Hello World! \"\n```\n\n### Methods  \n\n#### capitalize()  \n\n```swift\n\"hello world!\".capitalize() // \"Hello world!\"\n```\n\n#### casefold()  \n\n```swift\n\"ß\".casefold() // \"ss\"\n```\n\n#### center(width [,fillchar])  \n\n```swift\n\"1234\".center(10) // \"   1234   \"\n\"123\"center(10) // \"   123    \"\n\"1234\".center(10,fillchar:\"0\") // \"0001234000\"\n\"123\"center(10,fillchar:\"0\") // \"0001230000\"\n```\n\n#### count(sub [,start [,end]])  \n\n```swift\n\"abc abc abc\".count(\"abc\") // 3\n\"aaaaaaaaaa\".count(\"a\", start:2) // 8\n\"bbbbbbbbbb\".count(\"bb\", end:8) // 4\n```\n\n#### endswith(suffix [,start [,end]])  \n\n```swift\n\"hello world!\".endswith(\"!\") // true\n\"hello world!\".endswith(\"world!\")  // true\n\"hello\".endswith(\"world\") // false\n\"hello\".endswith([\"hello\",\"world\",\"!\"]) // true\n```\n\n#### expandtabs(tabsize=8)  \n\n```swift\n\"abc\\tabc\\t\".expandtabs() // \"abc        abc        \"\n\"abc\\tabc\\t\".expandtabs(4) // \"abc    abc    \"\n```\n\n#### find(sub [,start [,end]])  \n\n```swift\n\"123412312312345\".find(\"123\") // 0\n\"123412312312345\".find(\"12345\") // 10\n\"123412312312345\".find(\"5\") // 14\n\"123412312312345\".find(\"31\") // 6\n\"123412312312345\".find(\"0\") // -1\n```\n\n#### format(args...,kwargs)  \n\n**Available after v2.0**  \n\n```swift\n\"{}, {}, {}\".format(\"a\", \"b\", \"c\") // \"a, b, c\"\n\"{0}, {1}, {2}\".format(\"a\", \"b\", \"c\") // \"a, b, c\"\n\"{0}{1}{0}\".format(\"abra\", \"cad\") // \"abracadabra\"\n\"{:,}\".format(1234567890) // \"1,234,567,890\"\n```\n\nFormat specification\n\nhttps://docs.python.org/3/library/string.html#format-specification-mini-language\n\n#### format_map(kwargs) \n\n**Available after v2.0**  \n\n```swift\n\"{A}, {B}, {C}\".format([\"A\": \"a\", \"B\": \"b\", \"C\": \"c\"]) // \"a, b, c\"\n\"{number:,}\".format([\"number\":1234567890]) // \"1,234,567,890\"\n```\n\nFormat specification\n\nhttps://docs.python.org/3/library/string.html#format-specification-mini-language\n\n#### index(sub [,start [,end]]) throws  \n\n```swift\n\"123412312312345\".index(\"123\") // 0\n\"123412312312345\".index(\"12345\") // 10\n\"123412312312345\".index(\"5\") // 14\n\"123412312312345\".index(\"31\") // 6\n\"123412312312345\".index(\"0\") // throw PyException.ValueError\n```\n\n#### isalnum()  \n\n```swift\n\"123abc\".isalnum() // true\n\"１０００A\".isalnum() // true\n\"日本語\".isalnum() // true\n\"abc 123\".isalnum() // false\n```\n\n#### isalpha()  \n\n```swift\n\"I have pen.\".isalpha() // false\n\"qwerty\".isalpha() // true\n\"日本語\".isalpha() // true\n\"123\".isalpha() // false\n\"\".isalpha() // false\n```\n\n#### isascii()  \n\n```swift\n\"I have pen.\".isascii() // trur\n\"qwerty\".isascii() // true\n\"123\".isascii() // true\n\"\".isascii() // true\n\"非ASCII文字列\".isascii() // false\n```\n\n#### isdecimal()  \n\n```swift\n\"123\".isdecimal() // true\n\"１２３４５\".isdecimal() // true\n\"一\".isdecimal() // false\n\"\".isdecimal() // false\n```\n\n#### isdigit()  \n\n```swift\n\"123\".isdigit() // true\n\"１２３４５\".isdigit() // true\n\"一\".isdigit() // true\n\"\".isdigit() // true\n```\n\n#### islower()  \n\n```swift\n\"lower case string\".islower() // true\n\"Lower case string\".islower() // false\n\"lower case String\".islower() // false\n\"lower Case string\".islower() // false\n\"小文字では無い\".islower() // false\n```\n\n#### isnumeric()  \n\n```swift\n\"123\".isnumeric() // true\n\"１２３４５\".isnumeric() // true\n\"一\".isnumeric() // true\n\"\".isnumeric() // false\n```\n\n#### isprintable()  \n\n```swift\n\"\".isprintable() // true\n\"abc\".isprintable() // true\n\"\\u{060D}\".isprintable() // false\n```\n\n#### isspace()  \n\n```swift\n\" \".isspace() // true\n\"\".isspace() // false\n\"Speace\".isspace() // false\n```\n\n#### istitle()  \n\n```swift\n\"Title Case String\".istitle() // true\n\"Title_Case_String\".istitle() // true\n\"Title__Case  String\".istitle() // true\n\"not Title Case String\".istitle() // false\n\"NotTitleCaseString\".istitle() // false\n\"Not Title case String\".istitle() // false\n```\n\n#### isupper()  \n\n```swift\n\"UPPER CASE STRING\".isupper() // true\n\"Upper Case String\".isupper() // false\n\"大文字では無い\".isupper() // false\n```\n\n#### join(iterable)  \n\n```swift\nlet array = [\"abc\",\"def\",\"ghi\"]\n\"\".join(array) // \"abcdefghi\"\n\"-\".join(array) // \"abc-def-ghi\"\n\"++\".join(array) // \"abc++def++ghi\"\n```\n\n#### ljust(width [,fillchar])  \n\n```swift\n\"abc\".ljust(1) // \"abc\"\n\"abc\".ljust(5) // \"  abc\"\n\"abc\".ljust(5, fillchar:\"$\") // \"$$abc\"\n```\n\n#### lower()  \n\n```swift\n\"ABCDE\".lower() // \"abcde\"\n\"あいうえお\".lower() // \"あいうえお\"\n```\n\n#### lstrip(chars=nil)  \n\n```swift\n\"  lstrip sample\".lstrip() // \"lstrip sample\"\n\"  lstrip sample\".lstrip(\" ls\") // \"trip sample\"\n\"lstrip sample\".lstrip() // \"lstrip sample\"\n```\n\n#### maketrans(x [,y [,x]])  \n\n```swift\nString.maketrans([97:\"A\",98:nil,99:\"String\"]) // [\"a\":\"A\",\"b\":\"\",\"c\":\"String\"]\nString.maketrans([\"a\":\"A\",\"b\":nil,\"c\":\"String\"]) // [\"a\":\"A\",\"b\":\"\",\"c\":\"String\"]\nString.maketrans(\"abc\",y: \"ABC\") // [\"a\":\"A\",\"b\":\"B\",\"c\":\"C\"]\nString.maketrans(\"abc\", y: \"ABC\", z: \"xyz\") // [\"a\":\"A\",\"b\":\"B\",\"c\":\"C\",\"x\":\"\",\"y\":\"\",\"z\":\"\"]\n```\n\n#### partition(sep)  \n\n```swift\n\"a,b,c\".partition(\",\") // (\"a\",\",\",\"b,c\")\n\"a,b,c\".partition(\"x\") // (\"a,b,c\",\"\",\"\")\n```\n\n#### replace(old, new [,count])  \n\n```swift\n\"abc\".replace(\"bc\", new: \"bcd\") // \"abcd\"\n\"Python python python python\".replace(\"python\", new: \"Swift\", count: 2) // \"Python Swift Swift python\"\n```\n\n#### rfind(sub [,start [,end]])  \n\n```swift\n\"0123456789\".rfind(\"0\") // 0\n\"0123456789\".rfind(\"02\") // -1\n\"0123454321\".rfind(\"2\") // 8\n\"0123454321\".rfind(\"1\", end: -1) // 1\n```\n\n#### rindex(sub [,start [,end]]) throws  \n\n```swift\n\"0123456789\".rindex(\"0\") // 0\n\"0123456789\".rindex(\"02\") // throw PyException.ValueError\n\"0123454321\".rindex(\"2\") // 8\n\"0123454321\".rindex(\"1\", end: -1) // 1\n```\n\n#### rjust(width [,fillchar])  \n\n```swift\n\"abc\".rjust(1) // \"abc\"\n\"abc\".rjust(5) // \"abc  \"\n\"abc\"rjust(5,fillchar:\"z\") // \"abczz\"\n```\n\n#### rpartition(sep)  \n\n```swift\n\"a,b,c\".rpartition(\",\") // (\"a,b\", \",\", \"c\")\n\"a,b,c\".rpartition(\"x\") // (\"\", \"\", \"a,b,c\")\n```\n\n#### rsplit(sep=nil [,maxsplit])  \n\n```swift\n\"a,b,c,d,\".rsplit(\",\") // [\"a\", \"b\", \"c\", \"d\", \"\"]\n\"a,b,c,d,\".rsplit() // [\"a,b,c,d,\"]\n\"a,b,c,d,\".rsplit(\",\", maxsplit: 2) // [\"a,b,c\",\"d\", \"\"]\n\"a,b,c,d,\".rsplit(\",\", maxsplit: 0) // [\"a,b,c,d,\"]\n\"aabbxxaabbaaddbb\".rsplit(\"aa\", maxsplit: 2) // [\"aabbxx\", \"bb\", \"ddbb\"]\n```\n\n#### rstrip(chars=nil)  \n\n```swift\n\"rstrip sample   \".rstrip() // \"rstrip sample\"\n\"rstrip sample   \".rstrip(\"sample \") // \"rstri\"\n\"  rstrip sample\".rstrip() // \"  rstrip sample\"\n```\n\n#### split(sep=nil [,maxsplit])  \n\n```swift\n\"a,b,c,d,\".split(\",\") // [\"a\", \"b\", \"c\", \"d\", \"\"]\n\"a,b,c,d,\".split() // [\"a,b,c,d,\"]\n\"a,b,c,d,\".split(\",\", maxsplit: 2) // [\"a\", \"b\", \"c,d,\"]\n\"a,b,c,d,\".split(\",\", maxsplit: 0) // [\"a,b,c,d,\"]\n\"aabbxxaabbaaddbb\".split(\"aa\", maxsplit: 2) // [\"\", \"bbxx\", \"bbaaddbb\"]\n```\n\n#### splitlines([keepends])  \n\n```swift\n\"abc\\nabc\".splitlines() // [\"abc\", \"abc\"]\n\"abc\\nabc\\r\".splitlines(true) // [\"abc\\n\", \"abc\\r\"]\n\"abc\\r\\nabc\\n\".splitlines() // [\"abc\", \"abc\"]\n\"abc\\r\\nabc\\n\".splitlines(true) // [\"abc\\r\\n\", \"abc\\n\"]\n```\n\n#### startswith(prefix [,start [,end]])  \n\n```swift\n\"hello world!\".startswith(\"hello\") // true\n\"hello world!\".startswith(\"h\") // true\n\"hello\".startswith(\"world\") // flase\n\"hello\".startswith([\"hello\", \"world\", \"!\"]) // true\n```\n\n#### strip(chars=nil)  \n\n```swift\n\"   spacious   \".strip() // \"spacious\"\n\"www.example.com\".strip(\"cmowz.\") // \"example\"\n```\n\n#### swapcase()  \n\n```swift\n\"aBcDe\".swapcase() // \"AbCdE\"\n\"AbC dEf\".swapcase() // \"aBc DeF\"\n\"あいうえお\".swapcase() // \"あいうえお\"\n```\n\n#### title()  \n\n```swift\n\"Title letter\".title() // \"Title Letter\"\n\"title Letter\".title() // \"Title Letter\"\n\"abc  abC _ aBC\".title() // \"Abc  Abc _ Abc\"\n```\n\n#### translate(transtable)  \n\n```swift\nlet table = String.maketrans(\"\", y: \"\", z: \"swift\")\n\n\"I will make Python like string operation library\".translate(table)\n// \"I ll make Pyhon lke rng operaon lbrary\"\n```\n\n#### upper()  \n\n```swift\n\"abcde\".upper() // \"ABCDE\"\n\"あいうえお\".upper() // \"あいうえお\"\n```\n\n#### zfill()  \n\n```swift\n\"abc\".zfill(1) // \"abc\"\n\"abc\".zfill(5) // \"00abc\"\n\"+12\".zfill(5) // \"+0012\"\n\"-3\".zfill(5) // \"-0003\"\n\"+12\".zfill(2) // \"+12\"\n```\n\nFor more detail please refer below link  \n[https://docs.python.org/3/library/stdtypes.html#string-methods](https://docs.python.org/3/library/stdtypes.html#string-methods)  \n\n### Sliceable protocol  \n\n```swift\nprotocol Sliceable : Collection {\n    init() /* Required. */\n    subscript (_ start: Int?, _ stop: Int?, _ step: Int?) -\u003e Self { get }\n    subscript (_ start: Int?, _ end: Int?) -\u003e Self { get }\n    subscript (_ i:Int) -\u003e Self.Element { get } /* Required. */\n    subscript (_ slice: Slice) -\u003e Self { get }\n    mutating func append(_ newElement: Self.Element) /* Required. */\n}\n```\n\nWith the introduction of `SwiftyPyString`, `String` conforms to the `Sliceable` protocol.  \n\nBy conforming to `Sliceable` protocol, it can get partial sequences as introduced in [Slice String](#Slice-String).  \n\n\nIf the type you want to conform to `Sliceable` is conforms to `RangeReplaceableCollection`, it can be used simply by defining `subscript (_ i:Int) -\u003e Self.Element { get }`.  \n\nIn addition, if `associatedtype Index` of `Collection` is `Int`, you can conform to `Sliceable` with a very short code as follows, like `Array`.  \n\n```swift\nextension Array : Sliceable { }\n```\n\n```swift\nlet arr = [1, 2, 3, 4, 5]\n\narr[0, 3]\n// [0, 1, 2]\n\nlet slice = Slice(step:2)\n\narr[slice]\n// [1, 3, 5]\n\n```\n## License\n\nThis project published under the MIT license. See the [LICENSE](./LICENSE) file for more information.  \n","funding_links":["https://github.com/sponsors/ChanTsune"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchantsune%2Fswiftypystring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchantsune%2Fswiftypystring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchantsune%2Fswiftypystring/lists"}