Difference between revisions of "KOTLIN: String"
Onnowpurbo (talk | contribs) (Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_strings.htm The Kotlin String data type is used to store a sequence of characters. String values must be surrounded by do...") |
Onnowpurbo (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 9: | Line 9: | ||
Raw string is declared within triple quote (""" """) and may contain multiple lines of text without any escape characters. | Raw string is declared within triple quote (""" """) and may contain multiple lines of text without any escape characters. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ||
− | + | ||
− | } | + | Tipe data String Kotlin digunakan untuk menyimpan urutan karakter. Nilai string harus diapit oleh tanda kutip ganda ("") atau tanda kutip tiga (""" """). |
+ | |||
+ | Kami memiliki dua jenis string yang tersedia di Kotlin - satu disebut Escaped String dan lainnya disebut Raw String. | ||
+ | |||
+ | String yang lolos dideklarasikan dalam tanda kutip ganda (" ") dan dapat berisi karakter escape seperti '\n', '\t', '\b' dll. | ||
+ | |||
+ | String mentah dideklarasikan dalam tanda kutip tiga (""" """) dan dapat berisi beberapa baris teks tanpa karakter escape apa pun. | ||
+ | |||
+ | |||
+ | |||
+ | ===Example=== | ||
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | val escapedString : String = "I am escaped String!\n" | ||
+ | var rawString :String = """This is going to be a | ||
+ | multi-line string and will | ||
+ | not have any escape sequence"""; | ||
+ | |||
+ | print(escapedString) | ||
+ | println(rawString) | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | I am escaped String! | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | This is going to be a | + | |
− | multi-line string and will | + | I am escaped String! |
− | not have any escape sequence | + | This is going to be a |
+ | multi-line string and will | ||
+ | not have any escape sequence | ||
+ | |||
This is optional to specify the data type for a String, Kotlin can understand that the a variable is a String because of the given double or tripple quotes. | This is optional to specify the data type for a String, Kotlin can understand that the a variable is a String because of the given double or tripple quotes. | ||
If you want to create a String variable without assigning the value then you must specify the type while declaring the variable otherwise it will raise an error: | If you want to create a String variable without assigning the value then you must specify the type while declaring the variable otherwise it will raise an error: | ||
− | + | Ini opsional untuk menentukan tipe data untuk sebuah String, Kotlin dapat memahami bahwa variabel a adalah sebuah String karena tanda kutip ganda atau tiga kali lipat yang diberikan. | |
− | + | ||
+ | Jika Anda ingin membuat variabel String tanpa menetapkan nilai maka Anda harus menentukan jenisnya saat mendeklarasikan variabel jika tidak maka akan menimbulkan kesalahan: | ||
− | + | fun main(args: Array<String>) { | |
+ | val name : String | ||
+ | |||
+ | name = "Zara Ali" | ||
+ | |||
+ | println(name) | ||
+ | } | ||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Zara Ali | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Kotlin String Templates | + | |
+ | Zara Ali | ||
+ | |||
+ | ==Kotlin String Templates== | ||
+ | |||
Kotlin string templates are pieces of code that are evaluated and whose results are interpolated into the string. A template expression starts with a dollar sign ($) and may consist of either a name or an expression. | Kotlin string templates are pieces of code that are evaluated and whose results are interpolated into the string. A template expression starts with a dollar sign ($) and may consist of either a name or an expression. | ||
− | fun main(args: Array<String>) { | + | Template string Kotlin adalah potongan kode yang dievaluasi dan hasilnya diinterpolasi ke dalam string. Ekspresi template dimulai dengan tanda dolar ($) dan dapat terdiri dari nama atau ekspresi. |
− | + | ||
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | val name : String = "Zara Ali" | |
− | + | ||
− | } | + | println("Name - $name") // Using template with variable name |
+ | |||
+ | println("Name length - ${name.length}") // Using template with expression. | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Name - Zara Ali | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Name length - 8 | + | |
− | Kotlin String Object | + | Name - Zara Ali |
+ | Name length - 8 | ||
+ | |||
+ | ==Kotlin String Object== | ||
+ | |||
Kotlin String is an object, which contains a number of properties and functions that can perform certain operations on strings, by writing a dot character (.) after the specific string variable. | Kotlin String is an object, which contains a number of properties and functions that can perform certain operations on strings, by writing a dot character (.) after the specific string variable. | ||
We will see some of the important properties and functions in this chapter, remaining you can find in official documentation of Kotlin latest version. | We will see some of the important properties and functions in this chapter, remaining you can find in official documentation of Kotlin latest version. | ||
− | Kotlin String Indexes | + | Kotlin String adalah sebuah objek, yang berisi sejumlah properti dan fungsi yang dapat melakukan operasi tertentu pada string, dengan menulis karakter titik (.) setelah variabel string tertentu. |
+ | |||
+ | Kita akan melihat beberapa properti dan fungsi penting dalam bab ini, sisanya dapat Anda temukan di dokumentasi resmi Kotlin versi terbaru. | ||
+ | |||
+ | |||
+ | ==Kotlin String Indexes== | ||
+ | |||
Kotlin String can be treated as a sequence of characters or you can say String is an array of characters. You can access its element by specifying the index of the element using a square brackets. | Kotlin String can be treated as a sequence of characters or you can say String is an array of characters. You can access its element by specifying the index of the element using a square brackets. | ||
String indexes start with 0, so if you want to access 4th element of the string then you should specify index as 3 to access the 4th element. | String indexes start with 0, so if you want to access 4th element of the string then you should specify index as 3 to access the 4th element. | ||
− | |||
− | |||
− | |||
− | + | Kotlin String dapat diperlakukan sebagai urutan karakter atau Anda dapat mengatakan String adalah array karakter. Anda dapat mengakses elemennya dengan menentukan indeks elemen menggunakan tanda kurung siku. | |
− | + | ||
− | } | + | Indeks string dimulai dengan 0, jadi jika Anda ingin mengakses elemen ke-4 dari string maka Anda harus menentukan indeks sebagai 3 untuk mengakses elemen ke-4. |
+ | |||
+ | |||
+ | ===Example=== | ||
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | val name : String = "Zara Ali" | ||
+ | |||
+ | println(name[3]) | ||
+ | println(name[5]) | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | a | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | A | + | |
− | Kotlin String Length | + | a |
+ | A | ||
+ | |||
+ | ==Kotlin String Length== | ||
+ | |||
We can use length property of Kotlin string to find out its length. | We can use length property of Kotlin string to find out its length. | ||
Kotlin function count() also returns the length of a given string. | Kotlin function count() also returns the length of a given string. | ||
− | Example | + | Kita dapat menggunakan properti length dari string Kotlin untuk mengetahui panjangnya. |
− | fun main(args: Array<String>) { | + | |
− | + | Fungsi Kotlin count() juga mengembalikan panjang string yang diberikan. | |
+ | |||
+ | ===Example=== | ||
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | val name : String = "Zara Ali" | ||
+ | |||
+ | println("The length of name :" + name.length) | ||
+ | println("The length of name :" + name.count()) | ||
+ | |||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | The length of name :8 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | The length of name :8 | + | |
− | Kotlin String Last Index | + | The length of name :8 |
+ | The length of name :8 | ||
+ | |||
+ | ==Kotlin String Last Index== | ||
+ | |||
We can use lastIndex property of Kotlin string to find out the index of the last character in the char sequence. If a string is empty then it returns a -1. | We can use lastIndex property of Kotlin string to find out the index of the last character in the char sequence. If a string is empty then it returns a -1. | ||
− | Example | + | Kita dapat menggunakan properti lastIndex dari string Kotlin untuk mengetahui indeks karakter terakhir dalam urutan char. Jika string kosong maka ia mengembalikan -1. |
− | fun main(args: Array<String>) { | + | |
− | + | ||
+ | ===Example=== | ||
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | val name : String = "Zara Ali" | ||
+ | |||
+ | println("The index of last character in name :" + name.lastIndex) | ||
+ | } | ||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | The index of last character in name :7 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Changing Case of Strings | + | |
+ | |||
+ | The index of last character in name :7 | ||
+ | |||
+ | ==Changing Case of Strings== | ||
+ | |||
Kotlin provides toUpperCase() and toLowerCase() functions to convert a string into upper case and lower case respectively. | Kotlin provides toUpperCase() and toLowerCase() functions to convert a string into upper case and lower case respectively. | ||
− | Example | + | Kotlin menyediakan fungsi toUpperCase() dan toLowerCase() untuk mengonversi string menjadi huruf besar dan huruf kecil masing-masing. |
− | fun main(args: Array<String>) { | + | |
− | + | ||
+ | |||
+ | |||
+ | ===Example=== | ||
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | val name : String = "Zara Ali" | ||
+ | |||
+ | println("Upper case of name :" + name.toUpperCase()) | ||
+ | println("Lower case of name :" + name.toLowerCase()) | ||
+ | } | ||
− | |||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Upper case of name :ZARA ALI | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Lower case of name :zara ali | + | |
− | Kotlin String Concatenation | + | Upper case of name :ZARA ALI |
+ | Lower case of name :zara ali | ||
+ | |||
+ | ==Kotlin String Concatenation== | ||
+ | |||
We can use either + operator to concatenate two strings, or we can also use plus() function to concatenate two strings. | We can use either + operator to concatenate two strings, or we can also use plus() function to concatenate two strings. | ||
− | Example | + | Kita dapat menggunakan salah satu operator + untuk menggabungkan dua string, atau kita juga dapat menggunakan fungsi plus() untuk menggabungkan dua string. |
− | fun main(args: Array<String>) { | + | |
− | + | ||
− | + | ===Example=== | |
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | var firstName : String = "Zara " | ||
+ | var lastName : String = "Ali" | ||
+ | |||
+ | println("Full Name :" + firstName + lastName) | ||
+ | |||
+ | println("Full Name :" + firstName.plus(lastName) ) | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Full Name :Zara Ali | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Full Name :Zara Ali | + | |
− | Trim Characters from a String | + | Full Name :Zara Ali |
+ | Full Name :Zara Ali== | ||
+ | |||
+ | |||
+ | ==Trim Characters from a String | ||
+ | |||
We can remove first few or last few characters from a string using drop() or dropLast() functions. | We can remove first few or last few characters from a string using drop() or dropLast() functions. | ||
− | Example | + | Kita dapat menghapus beberapa karakter pertama atau terakhir dari sebuah string menggunakan fungsi drop() atau dropLast(). |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | var name : String = "Zara Ali" | ||
+ | |||
+ | println("Remove first two characters from name : " + name.drop(2)) | ||
+ | |||
+ | println("Remove last two characters from name : " + name.dropLast(2)) | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Remove first two characters from name : ra Ali | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Remove last two characters from name : Zara A | + | |
− | Quotes Inside a String | + | Remove first two characters from name : ra Ali |
+ | Remove last two characters from name : Zara A | ||
+ | |||
+ | ==Quotes Inside a String== | ||
+ | |||
To use quotes inside a string, use single quotes ('): | To use quotes inside a string, use single quotes ('): | ||
− | Example | + | Untuk menggunakan tanda kutip di dalam string, gunakan tanda kutip tunggal ('): |
− | fun main(args: Array<String>) { | + | |
− | + | ||
− | + | ===Example=== | |
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | var str1 : String = "That's it" | ||
+ | var str2 : String = "It's OK" | ||
+ | |||
+ | println("str1 : " + str1) | ||
+ | println("str2 : " + str2) | ||
+ | } | ||
− | |||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | str1 : That's it | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | str2 : It's OK | + | |
− | Finding a String inside a String | + | str1 : That's it |
+ | str2 : It's OK | ||
+ | |||
+ | ==Finding a String inside a String== | ||
+ | |||
Kotlin provides indexOf() function to find out a text inside a string. This function returns the index of the first occurrence of a specified text in a string | Kotlin provides indexOf() function to find out a text inside a string. This function returns the index of the first occurrence of a specified text in a string | ||
− | Example | + | Kotlin menyediakan fungsi indexOf() untuk mengetahui teks di dalam string. Fungsi ini mengembalikan indeks kemunculan pertama dari teks tertentu dalam sebuah string |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | var str : String = "Meditation and Yoga are synonymous with India" | ||
+ | |||
+ | println("Index of Yoga in the string - " + str.indexOf("Yoga")) | ||
+ | } | ||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Index of Yoga in the string - 15 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Comparing Two Strings | + | |
+ | Index of Yoga in the string - 15 | ||
+ | |||
+ | ==Comparing Two Strings== | ||
+ | |||
Kotlin provides compareTo() function to compare two strings. This function returns 0 if two strings are equal otherwise it will return 1. | Kotlin provides compareTo() function to compare two strings. This function returns 0 if two strings are equal otherwise it will return 1. | ||
− | Example | + | Kotlin menyediakan fungsi compareTo() untuk membandingkan dua string. Fungsi ini mengembalikan 0 jika dua string sama jika tidak maka akan mengembalikan 1. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
− | + | ||
+ | fun main(args: Array<String>) { | ||
+ | var str1 : String = "Apple" | ||
+ | var str2 : String = "Apple" | ||
+ | |||
+ | println(str1.compareTo(str2)) | ||
+ | } | ||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | 0 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Kotlin getOrNull() function | + | |
+ | 0 | ||
+ | |||
+ | ==Kotlin getOrNull() function== | ||
+ | |||
Kotlin getOrNull() function returns a character at the given index or null if the index is out of bounds of this char sequence. | Kotlin getOrNull() function returns a character at the given index or null if the index is out of bounds of this char sequence. | ||
− | Example | + | Fungsi Kotlin getOrNull() mengembalikan karakter pada indeks yang diberikan atau null jika indeks berada di luar batas urutan karakter ini. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | var name : String = "Zara" | ||
+ | |||
+ | println(name.getOrNull(0)) | ||
+ | println(name.getOrNull(2)) | ||
+ | println(name.getOrNull(100)) | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Z | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | r | + | |
− | null | + | Z |
− | Kotlin toString() function | + | r |
+ | null | ||
+ | |||
+ | ==Kotlin toString() function== | ||
+ | |||
Kotlin toString() function returns a string representation of the object.. | Kotlin toString() function returns a string representation of the object.. | ||
− | Example | + | Fungsi Kotlin toString() mengembalikan representasi string dari objek.. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
+ | |||
+ | fun main(args: Array<String>) { | ||
+ | var name : String = "Zara Ali" | ||
+ | |||
+ | println(name.toString()) | ||
+ | } | ||
− | |||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Zara Ali | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Quiz Time (Interview & Exams Preparation) | + | |
+ | Zara Ali | ||
+ | |||
+ | ==Quiz Time (Interview & Exams Preparation)== | ||
+ | |||
Q 1 - Which of the following is true about Control Flow Statement? | Q 1 - Which of the following is true about Control Flow Statement? | ||
Latest revision as of 10:23, 29 July 2022
Sumber: https://www.tutorialspoint.com/kotlin/kotlin_strings.htm
The Kotlin String data type is used to store a sequence of characters. String values must be surrounded by double quotes (" ") or triple quote (""" """).
We have two kinds of string available in Kotlin - one is called Escaped String and another is called Raw String.
Escaped string is declared within double quote (" ") and may contain escape characters like '\n', '\t', '\b' etc.
Raw string is declared within triple quote (""" """) and may contain multiple lines of text without any escape characters.
Tipe data String Kotlin digunakan untuk menyimpan urutan karakter. Nilai string harus diapit oleh tanda kutip ganda ("") atau tanda kutip tiga (""" """).
Kami memiliki dua jenis string yang tersedia di Kotlin - satu disebut Escaped String dan lainnya disebut Raw String.
String yang lolos dideklarasikan dalam tanda kutip ganda (" ") dan dapat berisi karakter escape seperti '\n', '\t', '\b' dll.
String mentah dideklarasikan dalam tanda kutip tiga (""" """) dan dapat berisi beberapa baris teks tanpa karakter escape apa pun.
Example
fun main(args: Array<String>) { val escapedString : String = "I am escaped String!\n" var rawString :String = """This is going to be a multi-line string and will not have any escape sequence"""; print(escapedString) println(rawString) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
I am escaped String! This is going to be a multi-line string and will not have any escape sequence
This is optional to specify the data type for a String, Kotlin can understand that the a variable is a String because of the given double or tripple quotes.
If you want to create a String variable without assigning the value then you must specify the type while declaring the variable otherwise it will raise an error:
Ini opsional untuk menentukan tipe data untuk sebuah String, Kotlin dapat memahami bahwa variabel a adalah sebuah String karena tanda kutip ganda atau tiga kali lipat yang diberikan.
Jika Anda ingin membuat variabel String tanpa menetapkan nilai maka Anda harus menentukan jenisnya saat mendeklarasikan variabel jika tidak maka akan menimbulkan kesalahan:
fun main(args: Array<String>) { val name : String name = "Zara Ali" println(name) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Zara Ali
Kotlin String Templates
Kotlin string templates are pieces of code that are evaluated and whose results are interpolated into the string. A template expression starts with a dollar sign ($) and may consist of either a name or an expression.
Template string Kotlin adalah potongan kode yang dievaluasi dan hasilnya diinterpolasi ke dalam string. Ekspresi template dimulai dengan tanda dolar ($) dan dapat terdiri dari nama atau ekspresi.
fun main(args: Array<String>) { val name : String = "Zara Ali" println("Name - $name") // Using template with variable name println("Name length - ${name.length}") // Using template with expression. }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Name - Zara Ali Name length - 8
Kotlin String Object
Kotlin String is an object, which contains a number of properties and functions that can perform certain operations on strings, by writing a dot character (.) after the specific string variable.
We will see some of the important properties and functions in this chapter, remaining you can find in official documentation of Kotlin latest version.
Kotlin String adalah sebuah objek, yang berisi sejumlah properti dan fungsi yang dapat melakukan operasi tertentu pada string, dengan menulis karakter titik (.) setelah variabel string tertentu.
Kita akan melihat beberapa properti dan fungsi penting dalam bab ini, sisanya dapat Anda temukan di dokumentasi resmi Kotlin versi terbaru.
Kotlin String Indexes
Kotlin String can be treated as a sequence of characters or you can say String is an array of characters. You can access its element by specifying the index of the element using a square brackets.
String indexes start with 0, so if you want to access 4th element of the string then you should specify index as 3 to access the 4th element.
Kotlin String dapat diperlakukan sebagai urutan karakter atau Anda dapat mengatakan String adalah array karakter. Anda dapat mengakses elemennya dengan menentukan indeks elemen menggunakan tanda kurung siku.
Indeks string dimulai dengan 0, jadi jika Anda ingin mengakses elemen ke-4 dari string maka Anda harus menentukan indeks sebagai 3 untuk mengakses elemen ke-4.
Example
fun main(args: Array<String>) { val name : String = "Zara Ali" println(name[3]) println(name[5]) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
a A
Kotlin String Length
We can use length property of Kotlin string to find out its length.
Kotlin function count() also returns the length of a given string.
Kita dapat menggunakan properti length dari string Kotlin untuk mengetahui panjangnya.
Fungsi Kotlin count() juga mengembalikan panjang string yang diberikan.
Example
fun main(args: Array<String>) { val name : String = "Zara Ali" println("The length of name :" + name.length) println("The length of name :" + name.count()) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
The length of name :8 The length of name :8
Kotlin String Last Index
We can use lastIndex property of Kotlin string to find out the index of the last character in the char sequence. If a string is empty then it returns a -1.
Kita dapat menggunakan properti lastIndex dari string Kotlin untuk mengetahui indeks karakter terakhir dalam urutan char. Jika string kosong maka ia mengembalikan -1.
Example
fun main(args: Array<String>) { val name : String = "Zara Ali" println("The index of last character in name :" + name.lastIndex) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
The index of last character in name :7
Changing Case of Strings
Kotlin provides toUpperCase() and toLowerCase() functions to convert a string into upper case and lower case respectively.
Kotlin menyediakan fungsi toUpperCase() dan toLowerCase() untuk mengonversi string menjadi huruf besar dan huruf kecil masing-masing.
Example
fun main(args: Array<String>) { val name : String = "Zara Ali" println("Upper case of name :" + name.toUpperCase()) println("Lower case of name :" + name.toLowerCase()) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Upper case of name :ZARA ALI Lower case of name :zara ali
Kotlin String Concatenation
We can use either + operator to concatenate two strings, or we can also use plus() function to concatenate two strings.
Kita dapat menggunakan salah satu operator + untuk menggabungkan dua string, atau kita juga dapat menggunakan fungsi plus() untuk menggabungkan dua string.
Example
fun main(args: Array<String>) { var firstName : String = "Zara " var lastName : String = "Ali" println("Full Name :" + firstName + lastName) println("Full Name :" + firstName.plus(lastName) ) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Full Name :Zara Ali Full Name :Zara Ali==
==Trim Characters from a String
We can remove first few or last few characters from a string using drop() or dropLast() functions.
Kita dapat menghapus beberapa karakter pertama atau terakhir dari sebuah string menggunakan fungsi drop() atau dropLast().
Example
fun main(args: Array<String>) { var name : String = "Zara Ali" println("Remove first two characters from name : " + name.drop(2)) println("Remove last two characters from name : " + name.dropLast(2)) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Remove first two characters from name : ra Ali Remove last two characters from name : Zara A
Quotes Inside a String
To use quotes inside a string, use single quotes ('):
Untuk menggunakan tanda kutip di dalam string, gunakan tanda kutip tunggal ('):
Example
fun main(args: Array<String>) { var str1 : String = "That's it" var str2 : String = "It's OK" println("str1 : " + str1) println("str2 : " + str2) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
str1 : That's it str2 : It's OK
Finding a String inside a String
Kotlin provides indexOf() function to find out a text inside a string. This function returns the index of the first occurrence of a specified text in a string
Kotlin menyediakan fungsi indexOf() untuk mengetahui teks di dalam string. Fungsi ini mengembalikan indeks kemunculan pertama dari teks tertentu dalam sebuah string
Example
fun main(args: Array<String>) { var str : String = "Meditation and Yoga are synonymous with India" println("Index of Yoga in the string - " + str.indexOf("Yoga")) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Index of Yoga in the string - 15
Comparing Two Strings
Kotlin provides compareTo() function to compare two strings. This function returns 0 if two strings are equal otherwise it will return 1.
Kotlin menyediakan fungsi compareTo() untuk membandingkan dua string. Fungsi ini mengembalikan 0 jika dua string sama jika tidak maka akan mengembalikan 1.
Example
fun main(args: Array<String>) { var str1 : String = "Apple" var str2 : String = "Apple" println(str1.compareTo(str2)) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
0
Kotlin getOrNull() function
Kotlin getOrNull() function returns a character at the given index or null if the index is out of bounds of this char sequence.
Fungsi Kotlin getOrNull() mengembalikan karakter pada indeks yang diberikan atau null jika indeks berada di luar batas urutan karakter ini.
Example
fun main(args: Array<String>) { var name : String = "Zara" println(name.getOrNull(0)) println(name.getOrNull(2)) println(name.getOrNull(100)) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Z r null
Kotlin toString() function
Kotlin toString() function returns a string representation of the object..
Fungsi Kotlin toString() mengembalikan representasi string dari objek..
Example
fun main(args: Array<String>) { var name : String = "Zara Ali" println(name.toString()) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Zara Ali
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following is true about Control Flow Statement?
A - Control flow controls the execution of the program
B - Loops and Decision Statements are part of control flow
C - Control flow is an essential part of modern programming languages
D - All of the above
Q 2 - Which of the following is a control flow statement in Kotlin?
A - String
B - Fun
C - When
D - None of the above
Q 3 - If we do not have control flow statements, then it will be almost impossible to write a computer program?
A - True
B - False