Welcome to the new Chapter, strings.
Let’s start by declaring a string variable, there’re 4 ways to do it:
x = 'LIGO detects first ever gravitational waves.' # using ' x = "LIGO detects first ever gravitational waves." # using " x = :LIGOdetectsfirstevergravitationalwaves # using : operator x = `LIGO detects first ever gravitational waves.` #we can use " & ' normally inside this type of strings.
For more about the operators in Ring go check this link here
Access String Letters:
device = 'iPhone' see device #output: i see device #output: P see device #output: h see device #output: o see device #output: n see device #output: e
NOTE: The index in Ring starts from 1 not 0 like the other languages.
Get the length of a string:
Do it just like the pythonic way.
device = 'iPhone' see len(device) #output : 6
Upper & Lower cases:
rip = 'rip' see upper(rip) + nl #output RIP see lower(rip) + nl #output rip
Left & Right functions:
We can get specific amount of text of a variable from the left or the right by using these functions:
Hello = `Hello, It's me.` see left(Hello, 5) + nl #output : Hello see right(Hello, 3) + nl #output : me.
NOTE: I used ` in declaring the string variable, it helps when the string contains ‘ and “.
I guess you already know what it does.
str = ` don't erase me. ` see trim(str) #output : don't erase me.
Copy() function (duplicating a string):
We can duplicate a string more than once using this function.
x = 'Bla ' see copy(x, 3) #output : Bla Bla Bla
When you have a multi-line string, you can know its lines count.
x = 'Apple Google Samsung' see lines(x) #output : 3
We’re done here, i hope the examples were clear enough.
See you in the next post.