|
| 1 | +package com.baeldung.stringtemplates |
| 2 | + |
| 3 | +/** |
| 4 | + * Example of a useful function defined in Kotlin String class |
| 5 | + */ |
| 6 | +fun padExample(): String { |
| 7 | + return "Hello".padEnd(10, '!') |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Example of a simple string template usage |
| 12 | + */ |
| 13 | +fun simpleTemplate(n: Int): String { |
| 14 | + val message = "n = $n" |
| 15 | + return message |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Example of a string template with a simple expression |
| 20 | + */ |
| 21 | +fun templateWithExpression(n: Int): String { |
| 22 | + val message = "n + 1 = ${n + 1}" |
| 23 | + return message |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Example of a string template with expression containing some logic |
| 28 | + */ |
| 29 | +fun templateWithLogic(n: Int): String { |
| 30 | + val message = "$n is ${if (n > 0) "positive" else "not positive"}" |
| 31 | + return message |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Example of nested string templates |
| 36 | + */ |
| 37 | +fun nestedTemplates(n: Int): String { |
| 38 | + val message = "$n is ${if (n > 0) "positive" else if (n < 0) "negative and ${if (n % 2 == 0) "even" else "odd"}" else "zero"}" |
| 39 | + return message |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Example of joining array's element into a string with a default separator |
| 44 | + */ |
| 45 | +fun templateJoinArray(): String { |
| 46 | + val numbers = listOf(1, 1, 2, 3, 5, 8) |
| 47 | + val message = "first Fibonacci numbers: ${numbers.joinToString()}" |
| 48 | + return message |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Example of escaping the dollar sign |
| 53 | + */ |
| 54 | +fun notAStringTemplate(): String { |
| 55 | + val message = "n = \$n" |
| 56 | + return message |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Example of a simple triple quoted string |
| 61 | + */ |
| 62 | +fun showFilePath(): String { |
| 63 | + val path = """C:\Repository\read.me""" |
| 64 | + return path |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Example of a multiline string |
| 69 | + */ |
| 70 | +fun showMultiline(): String { |
| 71 | + val receipt = """Item 1: $1.00 |
| 72 | +Item 2: $0.50""" |
| 73 | + return receipt |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Example of a multiline string with indentation |
| 78 | + */ |
| 79 | +fun showMultilineIndent(): String { |
| 80 | + val receipt = """Item 1: $1.00 |
| 81 | + >Item 2: $0.50""".trimMargin(">") |
| 82 | + return receipt |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Example of a triple quoted string with a not-working escape sequence |
| 87 | + */ |
| 88 | +fun showTripleQuotedWrongEscape(): String { |
| 89 | + val receipt = """Item 1: $1.00\nItem 2: $0.50""" |
| 90 | + return receipt |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Example of a triple quoted string with a correctly working escape sequence |
| 95 | + */ |
| 96 | + |
| 97 | +fun showTripleQuotedCorrectEscape(): String { |
| 98 | + val receipt = """Item 1: $1.00${"\n"}Item 2: $0.50""" |
| 99 | + return receipt |
| 100 | +} |
| 101 | + |
| 102 | +fun main(args: Array<String>) { |
| 103 | + println(padExample()) |
| 104 | + println(simpleTemplate(10)) |
| 105 | + println(templateWithExpression(5)) |
| 106 | + println(templateWithLogic(7)) |
| 107 | + println(nestedTemplates(-5)) |
| 108 | + println(templateJoinArray()) |
| 109 | + println(notAStringTemplate()) |
| 110 | + println(showFilePath()) |
| 111 | + println(showMultiline()) |
| 112 | + println(showMultilineIndent()) |
| 113 | + println(showTripleQuotedWrongEscape()) |
| 114 | + println(showTripleQuotedCorrectEscape()) |
| 115 | +} |
0 commit comments