Ring Documentation Walkthrough #8 – Strings

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[1] #output: i
see device[2] #output: P
see device[3] #output: h
see device[4] #output: o
see device[5] #output: n
see device[6] #output: e

NOTE: The index in Ring starts from 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 .


Trim() Function:

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

Lines() function:

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.

amrESSAM.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s