String Functions

SRON provides many string manipulation functions used to work on strings with great speed and performance.

Below are 19 functions to use:

  • at

  • count

  • delete

  • index

  • insert

  • len

  • push

  • pop

  • replace

  • reverse

  • reverse_sort

  • rindex

  • sort

  • split

  • subpart

  • tolower

  • toupper

  • trim

  • update


1. at():

This function is used to get the 'Char' value at a particular index.

{
    name : MAIN
     String str = "Hello, SRON!"

     Char val = at(str, 1)
     println(val)
}

OUTPUT:

e


2. count():

This function is used to count occurrence of 'Char' or 'String' value in parent String.

{
    name : MAIN
     String str = "Hello SRON Wassup? SRON is Best."

     Int val = count(str, " ")
     println(val)

     val = count(str, "SRON")
     println(val)
}

OUTPUT:

5 2


3. delete():

This function is used to delete a 'Char' value present in a particular index in a String.

{
    name : MAIN
     String str = "Hello, SRON!"

     delete(str,6)
     println(str)
}

OUTPUT:

Hello SRON!


4. index():

This function is used to find the index of first occurrence of a 'Char' value in a String. Returns -1, if character not found.

{
    name : MAIN
     String str = "Hello, SRON!"

     Int idx = index(str,'l')
     println(idx)
}

OUTPUT:

2


5. insert():

This function is used to insert a value into 'String'. It takes 3 arguments: first of type 'String', second of type 'Int' to specify at what index you want to insert the value, and third is the value you want to insert. It returns Void.

{
    name : MAIN
     String str = "Hello, SRON!"

     insert(str,6, "Hi")
     println(str)
}

OUTPUT:

Hello,Hi SRON!


6. len():

This function is used to get the length of the String.

{
    name : MAIN
     String str = "Hello, SRON!"

     Int len = len(str)
     println(len)
}

OUTPUT:

12


7. push():

This function is used to append a new String or Char at the end of the String. It takes two arguments: the String value in which you want to push and another one is a value of any type.

{
    name : MAIN
     String str = "Hello, SRON!"

     push(str," Hi")
     println(str)
}

OUTPUT:

Hello, SRON! Hi


8. pop():

This function is used to delete and return the last element of the String. It takes only one argument that is String and returns 'Char' value.

{
    name : MAIN
     String str = "Hello, SRON!"

     Char val = pop(str)
     println(val)
     println(str)
}

OUTPUT:

! Hello, SRON


9. replace():

This function is used to replace all the occurence of a particular character with another character. It takes three arguments: first is String value, second one is 'Char' value which will be replaced and third one is 'Char' value which will be replacement of second argument.

{
    name : MAIN
     String str = "Hello, SRON!"

     replace(str, 'l' '_')
     println(str)
}

OUTPUT:

He__o, SRON!


10. reverse():

This function is used to reverse a String. It takes only String as input and returns Void.

{
    name : MAIN
     String str = "Hello, SRON!"

     reverse(str)
     println(str)
}

OUTPUT:

!NORS ,olleH


11. rindex():

This function is used to get the index of a first character occurrence from the end. It takes two arguments: the String value from which you require to return the value and another argument is a 'Char' value for which you want to find the index. It returns -1, if the character is not found.

{
    name : MAIN
     String str = "Hello, SRON!"

     Int idx = rindex(str,'l')
     println(idx)
}

OUTPUT:

3


12. sort():

This function is used to sort a String. It returns a Void value.

{
    name : MAIN
     String str = "qwertyuiop"

     sort(str)
     println(str)
}

OUTPUT:

eiopqrtuwy


13. split():

This function is used to split the String on the basis of some character occurence. It returns a list filled with 'String' values.

{
    name : MAIN
     String str = "1-2-3-4-5-6-7-8-9"

     List lst = split(str,'-')
     println(lst)
}

OUTPUT:

[1, 2, 3, 4, 5, 6, 7, 8, 9]


14. subpart():

This function is used to extract a part of String. It takes 3 arguments: first is the 'String' from which you will extract the substring, then the 'Int' index by which you need to start extraction, then another 'Int' index by which you want to end the extraction.

{
    name : MAIN
     String str = "Hello, SRON!"

     String new_str = subpart(str, 7,11)
     println(new_str)
}

OUTPUT:

SRON


15. tolower():

This function is used to convert all the alphabets in the String to lowercase.

{
    name : MAIN
     String str = "Hello, SRON!"

     String new_str = tolower(str)
     println(new_str)
}

OUTPUT:

hello, sron!


16. toupper():

This function is used to convert all the alphabets in the String to uppercase.

{
    name : MAIN
     String str = "Hello, Sron!"

     String new_str = toupper(str)
     println(new_str)
}

OUTPUT:

HELLO, SRON!


17. trim():

This function is used to remove the leading and trailing newlines, tabs and spaces.

{
    name : MAIN
     String str = "\n\n Hello, SRON!\t"

     String trimmed_string = trim(str)
     println(trimmed_string)
}

OUTPUT:

Hello, SRON!


18. update():

This function is used to update a 'Char' value at a particular index.

{
    name : MAIN
    String str = "Hello  SRON!"

    update(str,5, ',')
    println(str)
}

OUTPUT:

Hello, SRON!


19. reverse_sort():

This function is used to sort a String in reverse order. It returns a Void value.

{
    name : MAIN
    String str = "SRON"

    reverse_sort(str)
    println(str)
}

OUTPUT:

NORS


Last updated