Kotlin StringThe String class represents an array of char types. Strings are immutable which means the length and elements cannot be changed after their creation. Unlike Java, Kotlin does not require a new keyword to instantiate an object of a String class. A String can be simply declared within double quote (" ") known as escaped string or triple quote(""" """) known as raw string. Kotlin String Property
String Function
String elements and templatesString elementsThe characters which are present in string are known as elements of string. Element of string are accessed by indexing operation string[index]. String's index value starts from 0 and ends at one less than the size of string string[string.length-1]. Index 0 represent first element, index 1 represent second element and so on. Example of accessing string element Output: H e ! String templatesString template expression is a piece of code which is evaluated and its result is returned into string. Both string types (escaped and raw string) contain template expressions. String templates starts with a dollar sign $ which consists either a variable name or an arbitrary expression in curly braces. String template as variable name: Output: i=10 String template as arbitrary expression in curly braces: String template is also used in arbitrary expression in curly braces to evaluate a string expression. This is done by using dollar sign $. abc is a string which length is 3 String template in raw string: Output: value 10 is greater than value 5 Kotlin String LiteralsKotlin has two types of string literals:
Escaped StringEscape String is declared within double quote (" ") and may contain escape characters like '\n', '\t', '\b' ,'\r','\$'etc. Raw StringRow String is declared within triple quote (""" """).It provides facility to declare String in new lines and contain multiple lines. Row String cannot contain any escape character. While using raw string with new line, it generates a | as margin prefix. For example: Output: Kotlin is official language |announce by Google for |android application development String trimMargin() functionLeading whitespace can be removed with trimMargin() function. By default, trimMargin() function uses | as margin prefix. Output: Kotlin is official language announce by Google for android application development However, it can be change by passing a new string inside trimMargin() function. Output: Kotlin is official language announce by Google for android application development Kotlin String EqualityIn Kotlin, strings equality comparisons are done on the basis of structural equality (==) and referential equality (===). In structural equality two objects have separate instances in memory but contain same value. Referential equality specifies that two different references point the same instance in memory. Structural equality (==)To check the two objects containing the same value, we use == operator or != operator for negation. It is equivalent to equals() in java. Output: true false Referential equality (===)To check the two different references point to the same instance, we use === operator. The !== operator is used for negation. a === b specifies true if and only if a and b both point to the same object. Let's see an example of referential equality to check different reference contains same instance or not. For creating string we are using a helper method buildString rather than using quotes. Output: false true Next TopicKotlin Exception Handling |