I want to manipulate strings in Kotlin like Python!

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.

Introduction

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.

String manipulation

slice

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

String search

//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.

String concatenation

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

String split

Split line
"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"]

trimming

//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"

Appearance count

"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

Zero padding

"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.

at the end

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

I want to manipulate strings in Kotlin like Python!
I want to do Dunnett's test in Python
I want to merge nested dicts in Python
I want to display the progress in Python!
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to do something in Python when I finish
I want to debug with Python
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I want to print in a comprehension
I tried to implement PLSA in Python 2
I want to use jar from python
I want to analyze logs with Python
I want to play with aws with python
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
I want to convert a table converted to PDF in Python back to CSV
I want to batch convert the result of "string" .split () in Python
Environment maintenance made with Docker (I want to post-process GrADS in Python
I want to do a monkey patch only partially safely in Python
3 ways to parse time strings in python [Note]
I want to use MATLAB feval with python
I want to pin Datetime.now in Django tests
I want to memoize including Python keyword arguments
I was able to recurse in Python: lambda
I want to email from Gmail using Python.
I wrote "Introduction to Effect Verification" in Python
I want to store DB information in list
I tried to implement TOPIC MODEL in Python
I want to use Temporary Directory with Python2
I want to use ceres solver from python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to sell Mercari by scraping python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I tried to implement selection sort in python
I want to write to a file with Python
Compare strings in Python
Reverse strings in Python
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
I want to create a priority queue that can be updated in Python (2.7)
I want to get the file name, line number, and function name in Python 3.4
[Python] I want to collect specific files scattered in each folder in one place
I tried to graph the packages installed in Python
Even beginners want to say "I fully understand Python"
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask