|
1 | 1 | package kotlin.js
|
2 | 2 |
|
3 |
| -public inline fun String.lastIndexOf(ch : Char, fromIndex : Int) : Int = lastIndexOf(ch.toString(), fromIndex) |
4 |
| -public inline fun String.lastIndexOf(ch: Char) : Int = lastIndexOf(ch.toString()) |
| 3 | +public inline fun String.nativeIndexOf(ch : Char, fromIndex : Int) : Int = nativeIndexOf(ch.toString(), fromIndex) |
| 4 | +public inline fun String.nativeLastIndexOf(ch : Char, fromIndex : Int) : Int = nativeLastIndexOf(ch.toString(), fromIndex) |
| 5 | + |
| 6 | +/** |
| 7 | + * Returns `true` if this string starts with the specified prefix. |
| 8 | + */ |
| 9 | +public fun String.startsWith(prefix: String, ignoreCase: Boolean = false): Boolean = |
| 10 | + regionMatches(0, prefix, 0, prefix.length(), ignoreCase) |
| 11 | + |
| 12 | +/** |
| 13 | + * Returns `true` if a substring of this string starting at the specified offset [thisOffset] starts with the specified prefix. |
| 14 | + */ |
| 15 | +public fun String.startsWith(prefix: String, thisOffset: Int, ignoreCase: Boolean = false): Boolean = |
| 16 | + regionMatches(thisOffset, prefix, 0, prefix.length(), ignoreCase) |
| 17 | + |
| 18 | +/** |
| 19 | + * Returns `true` if this string ends with the specified suffix. |
| 20 | + */ |
| 21 | +public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean = |
| 22 | + regionMatches(length() - suffix.length(), suffix, 0, suffix.length(), ignoreCase = true) |
| 23 | + |
5 | 24 |
|
6 |
| -public inline fun String.indexOf(ch : Char) : Int = indexOf(ch.toString()) |
7 |
| -public inline fun String.indexOf(ch : Char, fromIndex : Int) : Int = indexOf(ch.toString(), fromIndex) |
8 | 25 |
|
9 | 26 | public inline fun String.matches(regex : String) : Boolean {
|
10 | 27 | val result = this.match(regex)
|
11 | 28 | return result != null && result.size() > 0
|
12 | 29 | }
|
13 | 30 |
|
| 31 | +public fun String.equals(anotherString: String, ignoreCase: Boolean = false): Boolean = |
| 32 | + if (!ignoreCase) |
| 33 | + this == anotherString |
| 34 | + else |
| 35 | + this.toLowerCase() == anotherString.toLowerCase() |
| 36 | + |
| 37 | + |
| 38 | +public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean { |
| 39 | + if ((otherOffset < 0) || (thisOffset < 0) || (thisOffset > length() - length) |
| 40 | + || (otherOffset > other.length() - length)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return substring(thisOffset, thisOffset + length).equals(other.substring(otherOffset, otherOffset + length), ignoreCase) |
| 45 | +} |
| 46 | + |
| 47 | + |
14 | 48 | /**
|
15 | 49 | * Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this
|
16 | 50 | *
|
|
0 commit comments