0
0
Kotlinprogramming~30 mins

String methods (substring, split, trim) in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with String Methods in Kotlin
📖 Scenario: You are helping a small bookstore organize customer feedback. The feedback comes as a long string with customer names and their comments separated by commas. You want to clean and split this data to make it easier to read and analyze.
🎯 Goal: Build a Kotlin program that trims extra spaces from the feedback string, splits it into individual feedback entries, and extracts the customer names using substring.
📋 What You'll Learn
Create a string variable with the exact feedback text
Create a variable to hold the trimmed version of the feedback string
Split the trimmed string into a list of feedback entries
Extract customer names from each feedback entry using substring
Print the list of customer names
💡 Why This Matters
🌍 Real World
Cleaning and organizing customer feedback or any text data is common in businesses to understand opinions and improve services.
💼 Career
Knowing how to manipulate strings is essential for software developers, data analysts, and anyone working with text processing or user input.
Progress0 / 4 steps
1
Create the feedback string
Create a string variable called feedback with this exact value: " Alice: Loved the new books, Bob: Great service, Charlie: Will visit again "
Kotlin
Need a hint?

Use val feedback = "your string here" to create the string.

2
Trim the feedback string
Create a variable called trimmedFeedback that holds the result of trimming spaces from feedback using the trim() method
Kotlin
Need a hint?

Use val trimmedFeedback = feedback.trim() to remove spaces at the start and end.

3
Split the trimmed feedback into entries
Create a variable called entries that splits trimmedFeedback by comma and space ", " using the split() method
Kotlin
Need a hint?

Use val entries = trimmedFeedback.split(", ") to split the string into parts.

4
Extract and print customer names
Create a list called names by extracting the substring before the colon ':' from each entry in entries. Then print names. Use a for loop with variable entry and the substringBefore method.
Kotlin
Need a hint?

Use entry.substringBefore(':') inside a loop to get each name, then print the list.