AutolangDocs

Strings

A String is a sequence of characters used to represent text. In Autolang, strings come with a rich set of built-in methods for manipulation, searching, and type conversion.

Creating Strings

You can create strings using string literals (double quotes) or via the explicit String constructor. A very handy feature of the constructor is the ability to repeat a string multiple times.

// 1. String Literal val greeting = "Hello, World!" // 2. Empty String Constructor val empty = String() println("Is empty? ${empty.isEmpty()}") // 3. Repeat String Constructor val repeated = String("Ha", 3) println(repeated) // Output: HaHaHa

Character Literals & charAt

To minimize type-related errors, Autolang does not have a dedicated Char type. Instead, single-quote character literals (like 'a') evaluate directly to their integer (ASCII) values.

You can use get() to retrieve a 1-character String, or use the charAt() method to get the integer value directly.

val text = "abc" // get() returns a String println(text.get(0)) // "a" // charAt() returns an Int (ASCII value) println(text.charAt(0)) // 97 // Character literals evaluate to Int, perfect for 'when' expressions val x = 'b' println(when (x) { 'a' -> { "a" } else -> { "not a" // x is 'b' (98), so it falls to the else branch } })

Querying & Searching

Autolang provides straightforward methods to check the length of a string or search for substrings.

val text = "Autolang is fast" // Length println("Size: ${text.size()}") // Searching val hasLang = text.contains("lang") // true val index = text.indexOf("fast") // 12 // indexOf returns -1 if the substring is not found val notFound = text.indexOf("Python") // -1

Manipulation & Splitting

Strings are immutable. Methods that modify a string—like replace, trim, or substr—will always return a new string. You can also break a string into an Array of strings using split().

val dirtyString = " Clean me " println("'" + dirtyString.trim() + "'") // 'Clean me' val statement = "I like Java" val updated = statement.replace("Java", "Autolang") println(updated) // I like Autolang // Substrings val word = "Developer" println(word.substr(3)) // "eloper" (From index 3 to end) println(word.substr(0, 3)) // "Dev" (From index 0, length 3) // Splitting into an Array val csv = "apple,banana,orange" val fruits = csv.split(",") println("Second fruit: ${fruits[1]}") // banana

Type Conversions

Converting text into numeric types is extremely easy using the built-in toInt() and toFloat() methods.

val ageStr = "25" val ageInt = ageStr.toInt() val piStr = "3.14159" val piFloat = piStr.toFloat() val calculate = ageInt + 5 println("In 5 years, I will be ${calculate}")

Methods Reference

Constructors

  • String() - Creates an empty string.
  • String(str: String) - Creates a copy of the given string.
  • String(str: String, repeatTimes: Int) - Creates a new string by repeating the given string a specified number of times.

Querying

  • size(): Int - Returns the number of characters in the string.
  • isEmpty(): Bool - Returns true if the string has a length of 0.
  • get(position: Int): String - Returns the character at the specified index as a new String.
  • charAt(position: Int): Int - Returns the integer (ASCII) value of the character at the specified index.
  • contains(sub: String): Bool - Checks if the string contains the given substring.
  • indexOf(sub: String): Int - Returns the starting index of the first occurrence of the substring, or -1 if not found.

Manipulation (Returns a new String)

  • substr(from: Int): String - Extracts a portion of the string starting from the specified index to the end.
  • substr(from: Int, length: Int): String - Extracts a portion of the string starting from the specified index with the given length.
  • trim(): String - Removes leading and trailing whitespaces.
  • replace(old: String, new: String): String - Replaces all occurrences of the old substring with the new substring.
  • split(delimiter: String): Array<String> - Splits the string into an array of substrings using the specified delimiter.

Conversion

  • toInt(): Int - Parses the string and returns it as an integer.
  • toFloat(): Float - Parses the string and returns it as a floating-point number.