60 lines
1.1 KiB
Plaintext
60 lines
1.1 KiB
Plaintext
println("Some string manipulation");
|
|
|
|
text = "When I was young, everything was simpler.";
|
|
|
|
println("Our text looks like that:");
|
|
println(text);
|
|
|
|
x = 0;
|
|
while(x < 4)
|
|
{
|
|
print(text[x]);
|
|
x = x +1;
|
|
}
|
|
|
|
print("\n");
|
|
|
|
println("We can replace some content of the text, for example \" whatever \" ");
|
|
|
|
println("But first, let's try some simple replacement!");
|
|
|
|
text[5] = "M";
|
|
text[3] = "K";
|
|
|
|
println("Now text is: ", text);
|
|
|
|
println("Let's define some useful functions for us:");
|
|
|
|
def slice(text, from, to)
|
|
{
|
|
bit = "";
|
|
i = from;
|
|
while(i < to)
|
|
{
|
|
bit = bit + text[i];
|
|
i = i + 1;
|
|
}
|
|
return bit;
|
|
}
|
|
|
|
println("We defined slice, let\'s try it now!");
|
|
|
|
println(slice(text, 0, 8));
|
|
|
|
println("And some more: ", slice(text, 9, 14));
|
|
|
|
println("Sweet!");
|
|
println("What about replace function?");
|
|
|
|
def replace(text, replacement, start_position, end_position)
|
|
{
|
|
prefix = slice(text, 0, start_position);
|
|
suffix = slice(text, end_position, len(text));
|
|
result = prefix + replacement + suffix;
|
|
return result;
|
|
}
|
|
|
|
println("It looks pretty simple. Lets replace some text!");
|
|
|
|
println("Original: ", text);
|
|
println("After replacement: ", replace(text, "nice", 11, 16)); |