string in kotlin

kotlin char and string

String

A string is a set of words or complete sentences. To use a string in Kotlin, just like Java ,enclose the text in double quotes. If you want to integrate several lines of text, it is necessary to add three double quotes at the beginning and at the end (raw string).

val myString = “This string has only one line.” ;
val myLongString = “” “This string spans multiple lines.” “” ;

As in many programming languages, Kotlin allows the use of escape characters: a backslash allows you to designate a character that is not part of the string and should be treated as a control character. Conversely, a backslash also allows characters to be inserted into the string that normally have another meaning in Kotlin. The following escape characters are possible:

  • \ t: tabulation
  • \ b: backspace
  • \ n: new line
  • \ r: carriage return
  • \ ‘: Single quotes
  • \ “: Double quotes
  • \\: backslash
  • \ $: Dollar symbol

In the strings, the dollar symbol is used to indicate a tag. You can define it as a variable in a previous step. The tag is then replaced by a real value in the edition.

val author = “Sandra” ;
val myString = “This text was written by $ author” ;

Characters

For certain characters, kotlin also provides the specific character data type. Rather than putting it between double quotes like is string , we use single quotes.

Val c=’a’ ;

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

جميع الحقوق محفوظة لموقع كيفاش 2024