Ring/Lessons/Strings

From Wikibooks, open books for an open world
< Ring
Jump to: navigation, search

Strings[edit]

In this chapter we are going to learn about strings creation and manipulation.

String Literals[edit]

Syntax:

	cStr = "This is a string"
	cStr2 = 'Another string'
	cStr3 = :JustAnotherString
	cStr4 = `Yet "another" 'string' ! `

Get String Length[edit]

We can get the string length (letters count inside a string) using the len() function

Syntax:

	len(string) ---> string length

Example:

	cStr = "How are you?"
	see cStr + nl
	see "String size : " + len(cStr) + nl

Convert Letters Case[edit]

Syntax:

	lower(string) ---> convert string letters to lower case
	upper(string) ---> convert string letters to UPPER case

Example:

	cStr = "Welcome To The Ring Programming Language"
	see cStr + nl + upper(cStr) + nl + lower(cStr)

Access String Letters[edit]

We can access a letter inside a string by the letter index

Syntax:

	string[index] ---> get string letter
	string[index] = letter  # set string letter

Example:

	# print user name letter by letter (each letter in new line)

	See "Hello, Enter your name : " give cName
	for x = 1 to len(cName)
		see nl + cName[x]
	next


We can use for in to get string letters.


Example:

	# print user name letter by letter (each letter in new line)
	See "Hello, Enter your name : " give cName
	for x in cName
		see nl + x
	next

We can modify the string letters

Example:

	# convert the first letter to UPPER case
	See "Enter your name : " give cName
	cName[1] = upper(cName[1])
	see "Hello " + cName

Left() Function[edit]

We can get a specified number of characters from a string using the Left() function.

The starting position is 1.

Syntax:

	Left(string,count)

Example:

	see left("Hello World!",5) # print Hello

Right() Function[edit]

We can get a specified number of characters from a string using the Right() function.

The starting position is the last character on the right.

Syntax:

	Right(string,count)

Example:

	see Right("Hello World!",6) # print World!

Trim() Function[edit]

We can remove all leading and trailing spaces from a string using the Trim() function.

Syntax:

	trim(string)

Example:

	cMsg = "     Welcome      "
	see trim(cMsg)			# print Welcome

Copy() Function[edit]

We can duplicate a string more than one time using the copy() function.

Syntax:

	copy(string,count) ---> string replicqated count times

Example

	see copy("***hello***",3) # print ***hello******hello******hello***

Lines() Function[edit]

We can count the number of lines inside a string using the Lines() function.

Syntax:

	lines(string) ---> Number of lines inside the string

Example:

	cStr = "Hello
	How are you?
	are you fine?"
	see lines(cStr)		# print 3

Substr() Function[edit]

We can work on sub strings inside a string using the substr() function. Using Substr() we can

  • Find substring
  • Get substring from position to end
  • Get Number of characters from position
  • Transform Substring To Another Substring

Find substring[edit]

Syntax:

	substr(string,substring)  ---> the starting position of substring in string

Example:

	cStr = "Welcome to the Ring programming language"
	see substr(cStr,"Ring")		# print 16

Get substring from position to end[edit]

Syntax:

	substr(string,position)  ---> Get substring starting from position to end

Example:

	cStr = "Welcome to the Ring programming language"
	nPos = substr(cStr,"Ring")	# nPos = 16
	see substr(cStr,nPos)		# print Ring programming language

Get Number of Characters From Position[edit]

Syntax:

	substr(string,position,count)  ---> Get characters starting from position

Example:

	cStr = "Welcome to the Ring programming language"
	nPos = substr(cStr,"Ring")	# nPos = 16
	see substr(cStr,nPos,4)		# print Ring

Transform Substring To Another Substring[edit]

Syntax:

	substr(string,substring,newsubstring)  ---> Transformed string (Match case)
	substr(string,substring,newsubstring,1)  ---> Transformed string (Ignore case)

Example:

	cStr = "Welcome to the New programming language"
	see substr(cStr,"New","Ring") + nl  # print Welcome to the Ring programming language 
	see substr(cStr,"new","Ring",1)+ nl # print Welcome to the Ring programming language

strcmp() Function[edit]

We can compare between two strings using the strcmp() function.

Syntax:

	strcmp(cString1,cString2) ---> value = 0 if cString1 = cString2
				       value < 0 if cString1 < cString2
				       value > 0 if cString1 > cString2

Example:

	see strcmp("hello","hello") + nl +
	    strcmp("bcd","abc") + nl

Output:

	0
	-1
	1

str2list() and list2str() Functions[edit]

We can convert string lines to list items using the str2list() function. Also we can convert the list to a string using list2str() function.

Syntax:

	str2list(string) ---> list contains the string lines 
	list2str(list)   ---> string contains the list items

Example:

	/* output:
	** Items : 4
	** Item : Hello
	** Item : How are you ?
	** Item : are you fine ?
	** Item : ok
	** list2Str result = Hello
	** How are you ?
	** are you fine ?
	** ok
	** Done
	*/

	mystr = "Hello
	How are you ?
	are you fine ?
	ok"

	mylist = str2list(mystr)
	see "Items : " + len(mylist) + nl

	for x in mylist
		see "Item : " + x + nl
	next

	newstr = list2str(mylist)
	see "list2Str result = " + newstr

	if mystr = newstr
		see nl + "Done"
	else
		see nl + "Error!"
	ok