String Manipulation in Rui

Strings are sequences of characters used to represent text in Rui. They support indexing, slicing, and various built-in methods for powerful text manipulation.

Creating Strings

In Rui, strings are created by enclosing text in double quotes. Use suppose to declare string variables:

// Creating strings
suppose name = "Alice"
suppose message = "Hello, World!"
suppose empty = ""

// Displaying strings
write(name)        // Output: Alice
write(message)     // Output: Hello, World!
write(empty)       // Output: (empty line)

String Indexing

Access individual characters in a string using square brackets. Rui supports both positive and negative indexing:

suppose text = "Hello World"

write("First character: ", text[0])    // H
write("Last character: ", text[-1])    // d
write("Character at index 6: ", text[6])  // W

String Slicing

Extract parts of a string using slicing with start and end indices:

suppose text = "Hello World"

write("First 5: ", text[:5])      // Hello
write("Last 5: ", text[-5:])      // World
write("Middle: ", text[2:8])      // llo Wo
write("From index 6: ", text[6:]) // World

String Methods

Rui provides several built-in methods for string manipulation:

suppose text = "  Hello World  "

// Case conversion
write("Upper: ", text.upper())    //   HELLO WORLD
write("Lower: ", text.lower())    //   hello world

// Whitespace
write("Trimmed: ", text.trim())   // Hello World

// Splitting
suppose words = text.split(" ")   // ["", "", "Hello", "World", "", ""]

// Replacement
write("Replaced: ", text.replace("World", "Rui"))  //   Hello Rui

// Checking
write("Starts with 'Hello': ", text.startsWith("Hello"))  // false (due to spaces)
write("Ends with 'World': ", text.endsWith("World"))      // false (due to spaces)
write("Contains 'World': ", text.indexOf("World") >= 0)   // true

String Operations

Strings support various operations including concatenation and repetition:

// Concatenation
suppose result = "Hello" + " " + "World"  // Hello World

// Repetition
suppose repeated = "Hello" * 3            // HelloHelloHello

// Mixed types
suppose formatted = "Age: " + 25          // Age: 25

Built-in String Functions

Rui provides several built-in functions for string manipulation:

  • length(value): Get length of strings or arrays
  • join(array, delimiter): Join array elements with delimiter
  • parseInt(string): Convert string to integer
  • parseFloat(string): Convert string to float
suppose arr = ["a", "b", "c"]
write("Joined: ", join(arr, "-"))        // a-b-c

suppose numStr = "123"
write("Parsed: ", parseInt(numStr))      // 123

suppose text = "Hello"
write("Length: ", length(text))          // 5

Advanced String Processing

Here's a practical example of text processing using string methods:

// Process a sentence
suppose sentence = "The quick brown fox jumps over the lazy dog"
write("Original: ", sentence)

// Split into words
suppose words = sentence.split(" ")
write("Words: ", words)
write("Word count: ", length(words))

// Find longest word
suppose longest = words[0]
suppose i = 1
until (i >= length(words)) {
    if (length(words[i]) > length(longest)) {
        longest = words[i]
    }
    i = i + 1
}
write("Longest word: ", longest)

// Join with different delimiter
write("Joined with dashes: ", join(words, "-"))

String Comparison

Compare strings using comparison operators:

suppose name1 = "Alice"
suppose name2 = "Bob"
suppose name3 = "Alice"

// Equality
write(name1 == name2)  // false
write(name1 == name3)  // true

// Lexicographic comparison
write(name1 < name2)   // true (Alice comes before Bob)
write(name1 > name2)   // false

Method Chaining

Rui supports method chaining for more concise code:

suppose text = "  Hello World  "

// Chain multiple methods
suppose result = text.trim().lower().replace("world", "rui")
write(result)  // hello rui

// More complex chaining
suppose processed = "  THE QUICK BROWN FOX  "
suppose final = processed.trim().lower().replace(" ", "-")
write(final)   // the-quick-brown-fox

Best Practices

  • Use suppose for declarations: Always use suppose when declaring new string variables
  • Handle whitespace: Use trim() to clean user input
  • Check bounds: Be careful with indexing to avoid out-of-bounds errors
  • Use method chaining: Chain methods for cleaner, more readable code

String manipulation is a core feature of Rui, providing powerful tools for text processing and data handling. Practice these concepts to master string operations in Rui!