The string operation of kotlin is also convenient, but after all I want to manipulate the string like Python.
That's why I made a library that allows kotlin to perform string operations equivalent to Python.
ktPyString https://github.com/ChanTsune/ktPyString
By defining an extension function for String, kotlin can also operate strings like python.
Gradle
build.gradle
dependencies {
...
implementation 'dev.tsune:ktPyString:0.0.0'
...
}
Maven
<dependency>
<groupId>dev.tsune</groupId>
<artifactId>ktPyString</artifactId>
<version>0.0.0</version>
<type>pom</type>
</dependency>
Add to the dependent library.
val str = "0123456789"
str[0,5]
// 01234
str[0,8,2]
// 0246
str[null,null,-1]
// 9876543210
This is a slice operation for Pythonista. Once you get used to it, you will want to do it in other languages.
By the way, if you write the same operation in Python, it will be as follows.
str = "0123456789"
str[0:5]
# 01234
str[0:8:2]
# 0246
str[::-1]
# 9876543210
//Search from the beginning
"123412312312345".find("123") // 0
//Search by specifying the start position
"123412312312345".find("123",start:2) // 4
//Search by specifying the end position
"123412312312345".find("123",end:1) // -1
//Search from the end
"123412312312345".rfind("123") // 10
You can also search from the end by specifying the start position and end position.
val array = ["abc","def","ghi"]
"".join(array) // "abcdefghi"
"-".join(array) // "abc-def-ghi"
"++".join(array) // "abc++def++ghi"
"abc\nabc".splitlines() // ["abc", "abc"]
"abc\r\nabc\n".splitlines() // ["abc", "abc"]
//Split leaving a newline character
"abc\nabc\r".splitlines(true) // ["abc\n", "abc\r"]
"abc\r\nabc\n".splitlines(true) // ["abc\r\n", "abc\n"]
//Right end only
"rstrip sample ".rstrip() // "rstrip sample"
"rstrip sample ".rstrip("sample ") // "rstri"
" rstrip sample".rstrip() // " rstrip sample"
//Only on the left end
" lstrip sample".lstrip() // "lstrip sample"
" lstrip sample".lstrip(" ls") // "trip sample"
"lstrip sample".lstrip() // "lstrip sample"
//both ends
" spacious ".strip() // "spacious"
"www.example.com".strip("cmowz.") // "example"
"abc abc abc".count("abc") // 3
//Specifying the start position
"abc abc abc".count("abc", start:2) // 2
//Specifying the end position
"abc abc abc".count("abc", end:1) // 0
"abc".zfill(1) // "abc"
"abc".zfill(5) // "00abc"
//If signed
"+12".zfill(5) // "+0012"
"-3".zfill(5) // "-0003"
"+12".zfill(2) // "+12"
If signed, there will be a zero after the sign.
If you write everything, it will be long, so I will introduce it around here.
In addition to this, the methods available in Python's str type support most of the methods except those that are linguistically unrealizable or difficult to implement.
If you started programming from Python, isn't it relatively convenient because you will be able to operate strings in Python that you are familiar with?
You can implement this method, isn't this implementation better in performance? If it is kotlin, please let me know if it is beautiful to write like this.
We are waiting for you.
If so, I'd be happy to report a bug.
As an aside, since I made a Swift version library in the past, it may be possible to move most of the processing around string operations on iOS and Android by copying. (Because the grammar of Swift and kotlin is quite similar) https://qiita.com/ChanTsune/items/bd611a4c778c0fb338e6
Recommended Posts