{ }codelper

Learn › Craykon

Going further · 19

Working with strings

Slicing, finding, replacing, trimming, comparing and padding.

Once input comes in, you often trim it (drop spaces at both ends), find and cut out the part you need, and change its shape before printing.
The example below uses those tools one by one.

text = Console.ReadLine().Trim();date = Console.ReadLine().Trim();ab = Console.ReadLine().Split();n = Console.ReadLine().Trim();Console.WriteLine("[" + text + "]");Console.WriteLine(text.Substring(0, 5));i = text.IndexOf("World");Console.WriteLine(i);Console.WriteLine(text.Substring(i, 5));Console.WriteLine(text.IndexOf("Bye"));Console.WriteLine(date.Replace("-", "/"));if (ab[0] < ab[1]){    Console.WriteLine(-1);}else if (ab[0] > ab[1]){    Console.WriteLine(1);}else{    Console.WriteLine(0);}Console.WriteLine(n.PadLeft(3, "0"));
stdin
   Hello, World   
2026-11-10
pear apple
7
stdout
[Hello, World]
Hello
7
World
-1
2026/11/10
1
007

Key points

  • Trimming drops spaces at both ends.
    Trimming every line you read is the safe habit.
  • Finding returns the position of the first match.
    With no match most languages give -1, but not all.
  • When slicing, the second number means different things: an end position (not included) in some languages, a length in others.
  • Replacing changes every match in most languages, but JavaScript replace changes only the first.
  • Comparing returns negative, 0 or positive.
    Trust only the sign; the size means nothing.
  • Padding fills the front to a fixed width, turning 7 into 007.

How each language writes it

Pythonstrip(), find (-1 if missing; index raises an error), t[7:12] (end position), replace (all), rjust(3, "0")
JavaScripttrim(), indexOf, slice(7, 12) (end position), replace first only and replaceAll for all, padStart(3, "0").
localeCompare follows the locale
TypeScriptSame as JavaScript
Javastrip(), indexOf, substring(7, 12) (end position), replace (all; replaceAll takes a regex), compareTo (trust only the sign)
C#Trim(), IndexOf, Substring(7, 5) (length), Replace (all), PadLeft(3, '0').
string.Compare and IndexOf(string) follow the culture; for raw character codes add StringComparison.Ordinal or use string.CompareOrdinal
C++No trim, so cut it yourself.
find (std::string::npos if missing), substr(7, 5) (length), compare.
Replace-all is a loop.
Pad with setw and setfill
CNo trim or replace-all; do them yourself.
strstr (NULL if missing), slice with printf("%.5s", text + 7) (start address and length), compare with strcmp, pad with printf("%03d")
CraykonTrim(), IndexOf (-1 if missing), Substring(7, 5) (length), Replace (all), PadLeft(3, "0")

Common mistakes

Mixing up the second slicing numberC# · Java · JavaScript

"Hello, World".Substring(7, 12)  // C#
"Hello, World".substring(7, 5)   // Java, JavaScript

→ C# throws an out-of-range error, and so does Java. JavaScript quietly swaps the two numbers and returns ", "

Check whether the second number is an end position or a length in that language.

Expecting replace to change every matchJavaScript · TypeScript

console.log("2026-11-10".replace("-", "/"));

→ 2026/11-10 — only the first one changed

Use replaceAll for all of them.
Watch out in Java too: replaceAll takes a regex, so "a.b".replaceAll(".", "-") is "---".

Expecting compareTo to return 1Java · C · C++ · C#

System.out.println("pear".compareTo("apple"));

→ 15 — it is 'p' - 'a'

Look only at the sign, as in > 0 or < 0.

How languages differ
Input made on Windows ends lines with \r\n.
Java nextLine(), C# ReadLine() and Craykon ReadLine() strip the \r, but Python input() (on Linux and macOS), JavaScript split("\n"), C fgets and C++ getline keep it, so "World\r" == "World" is false; trim the lines you read.

Practice