String Features

String Features


String interpolation lets you embed expressions directly inside strings using f-string syntax. It's cleaner and more readable than concatenation.

F-String Syntax

Test Coverage: ✅ tests/lang_comp/strings/interpolation.rs

Try It in the Notebook

Expected Output: "The sum of 10 and 20 is 30"

Expressions in F-Strings

Any expression can go inside {}:

Expected Output: "Total: $29.97"

Function Calls

Expected Output: "Message: Hello, Bob!"

Method Calls

Expected Output: "Uppercase: HELLO WORLD"

Multiple Expressions

Expected Output: "5 + 10 = 15, 10 + 15 = 25"

Nested F-Strings

Expected Output: "User: Alice from Boston"

F-Strings vs Concatenation

Method Syntax Readability Performance
F-String f"Hello {name}" High Fast
Concatenation "Hello " + name Medium Fast
Format "Hello {}".format(name) Medium Slower

Common Patterns

Logging

Expected Output: "[1234567890] INFO: Server started"

Error Messages

Expected Output: (errors for invalid, success message for valid)

URLs and Queries

Expected Output: "https://api.example.com/users/123?format=json"

SQL Queries (Careful!)

Expected Output: "SELECT * FROM users WHERE id = 42"

Security Note: Always use parameterized queries for user input!

JSON-Like Strings

Expected Output: '{"id": 1, "name": "Alice", "active": true}'

Formatting Numbers

Decimal Precision

Expected Output: "Pi: 3.14", "Pi: 3.1416"

Padding and Alignment

Expected Output: " 42", "00042"

Percentages

Expected Output: "Success rate: 85.6%"

Escaping Braces

Use double braces to include literal { or }:

Expected Output: "Set notation: {1, 2, 3}"

Multi-Line F-Strings

Expected Output:

Name: Alice
Age: 30
City: Boston

Debugging with F-Strings

Print Variable Names and Values

Expected Output: "x = 10, y = 20, x + y = 30"

Debug Expressions

Expected Output: "Length: 5, Sum: 15"

Performance Considerations

F-strings are compiled at parse time:

Best Practices

✅ Use F-Strings for Readability

✅ Keep Expressions Simple

✅ Be Careful with Security

Summary

Feature Status: WORKING
Test Coverage: 100%
Mutation Score: 96%

F-strings provide elegant, readable string interpolation by embedding expressions directly in string literals using {expression} syntax.

Key Takeaways:

  • Syntax: f"text {expression} text"
  • Any expression works: variables, functions, operators
  • Better readability than concatenation
  • Compiled at parse time (fast)
  • Use double braces {{ for literal braces
  • Never use with untrusted input in SQL/commands

← Previous: Error Handling | Next: String Methods →


Ruchy provides a rich set of string methods for manipulation, searching, and transformation.

Case Conversion

to_upper() - Uppercase

Expected Output: "HELLO WORLD"

to_lower() - Lowercase

Expected Output: "hello world"

Test Coverage: ✅ tests/lang_comp/strings/methods.rs

Try It in the Notebook

Expected Output: "ALICE"

Trimming Whitespace

trim() - Remove Leading/Trailing Whitespace

Expected Output: "hello world"

trim_left() - Remove Leading Whitespace

Expected Output: "hello"

trim_right() - Remove Trailing Whitespace

Expected Output: "world"

Length and Checking

len() - String Length

Expected Output: 5

is_empty() - Check if Empty

Expected Output: true, false

Searching

contains() - Check Substring

Expected Output: true, false

starts_with() - Check Prefix

Expected Output: true, false

ends_with() - Check Suffix

Expected Output: true, false

index_of() - Find Position

Expected Output: 6, -1

Splitting and Joining

split() - Split by Delimiter

Expected Output: ["apple", "banana", "cherry"]

lines() - Split by Newlines

Expected Output: ["line1", "line2", "line3"]

join() - Join Array with Separator

Expected Output: "hello world !", "helloworld!"

Replacement

replace() - Replace All Occurrences

Expected Output: "hi world hi"

replace_first() - Replace First Occurrence

Expected Output: "hi world hello"

Slicing

Substring by Range

Expected Output: "hello", "world", "hello", "world"

substring() - Extract Substring

Expected Output: "hello", "world"

Character Access

Indexing

Expected Output: "h", "e", "o"

chars() - Get Character Array

Expected Output: ["h", "e", "l", "l", "o"]

Repeating

repeat() - Repeat String

Expected Output: "hahaha"

Padding

pad_left() - Left Padding

Expected Output: "00042"

pad_right() - Right Padding

Expected Output: "42000"

Reversing

reverse() - Reverse String

Expected Output: "olleh"

Common Patterns

Email Validation

Expected Output: true, false

URL Parsing

Expected Output: "https", "example.com", "/path/to/resource"

CSV Parsing

Expected Output: [["Alice", "30", "Boston"], ["Bob", "25", "NYC"], ["Carol", "35", "LA"]]

Title Case

Expected Output: "Hello World"

Slug Generation

Expected Output: "hello-world-example"

Word Count

Expected Output: 3

Truncate with Ellipsis

Expected Output: "This is a ..."

Remove Punctuation

Expected Output: "Hello world"

Extract Numbers

Expected Output: "123456"

Chaining Methods

Expected Output: "hello rust"

Complex Example

Expected Output: "ALICE | BOB | CAROL"

Comparison

== - Equality

Expected Output: true, false

Case-Insensitive Comparison

Expected Output: true

Lexicographic Comparison

Expected Output: true, true

Best Practices

✅ Chain Methods for Clarity

✅ Use Descriptive Variable Names

✅ Validate Input

✅ Use String Methods Over Regex When Possible

Performance Tips

  • contains() is faster than regex for simple substring checks
  • Use split() once and reuse the array instead of multiple splits
  • trim() is cheaper than regex-based whitespace removal
  • String concatenation with + is fine for small strings, use arrays and join() for many strings

Summary

Feature Status: WORKING
Test Coverage: 100%
Mutation Score: 97%

Ruchy strings come with a comprehensive set of methods for manipulation, searching, and transformation. Use them to write clean, readable string processing code.

Key Takeaways:

  • Case: to_upper(), to_lower()
  • Trim: trim(), trim_left(), trim_right()
  • Search: contains(), starts_with(), ends_with(), index_of()
  • Split/Join: split(), join(), lines()
  • Replace: replace(), replace_first()
  • Chain methods for readable transformations
  • Validate input before processing

← Previous: String Interpolation | Next: String Escaping →