Monday, May 29, 2017

Kotlin: working with String

Welcome to kotlin Series




In this tutorial, we are working with String of Kotlin.
In this post, we are going to learn about-
#variable type
#variable declaring
#String
#Join String
#Compare two String
#Find index from any String.

Ok Let's start by Creating the main method
fun main(arg : Array<String>){
  println("Hello world kotlin")
}

In Kotlin we have two types of variable-
1.mutable
2.immutable

1.mutable variable can change after declaring
2.immutable can not change Declare a string

the mutable variable can declare as using prefix val and
the immutable variable can declare as using prefix var

Let's add two type of variable-
val name = "shudipto"
var id = 1215

Defina a string-
val name = "Shudipto"
you can also declare a multiline string by using triple double quotation (""")
val des ="""I am shudipto trafder lives in Khulna.
  I study on the Khulna University """
Now print a string-
println("Name: "+name)
we can print a string in another way. we use dollar sign $ and directly type the variable name that's we want to print in the double quotation -
println("Name: $name")
Note: you probably confuse with semi-clone but Kotlin does not care about semi-clone.

Now join two String-
we take two string in two variable and join those variables into a new variable and print this newly created variable.
val FName = "Shudipto"
val LName = "Trafder"
    
var fullName = FName + LName
    
println("Full name: "+fullName)
we can use the alternative methods directly joint variable int the print statement-
 val FName = "Shudipto"
 val LName = "Trafder"

 println("Full name: ${FName+" "+LName}")
Compare two String- we declare two string and compare with equals function or using '=='
    //compare two string
    val string1 = "user"
    val string2 = "User"

    println("Strings are same: ${string1.equals(string2)}")
    println("Strings are same: ${string1 == string2}")
If you are working with ignore case then use-
println("Strings are same: ${string1.equals(string2,true)}")
Contains a word in particular String- we add a String like "I love you". we are checking that this String contains love word. Let jump on code-
var s = "I love you"

//check a word that exists in a string
println("Word is exist: ${s.contains("love")}")
the result will be shown in a boolean value.

Today's Last Topics. Find an Index of any String.
First, we declare a String "I love you". We are going to find what char is in the position
var s = "I love you"

//get a character with specific index
println("2nd index: ${s[2]}")
If we want to find a specific range of the index. For example, we want to find the index position 2 to 5. On code
var s = "I love you"
    

//a specific index from 2 to 5
println("2nd index to 5 Index: ${s.subSequence(2,6)}")

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :