Project

General

Profile

Actions

Useful string functions

  • Remove the last character of a string :
fun removeLastChar (s)=
    substr s 0 (strlen s)-1;;
  • Compare the last character. Return 1 if it is the same.
fun testLastChar (s, c)=
    let substr s (strlen s)-1 1 -> ch in
    !strcmp c ch;;
  • Return the position of the last occurence of a character
fun getLastPosChar (string, char)=
         let 0 -> i in
         let (strfind char string 0) + 1 -> npos in
         while npos != nil do
                (
                set i = npos;
                set npos = (strfind char string npos) + 1;
                i
                );;
  • Return the position of the last /
fun getLastPosSlash (s)=
  if s == nil then nil else
    let strlen s -> n in
    let n-1 -> i in
    (
     while (i >=0) && ((nth_char s i) != '/) do
        set i = i-1;
     i
    );;
  • Return the position of the last .
fun getLastPosPoint (s)=
  if s == nil then nil else
    let (strlen s) - 1 -> i in
    (
      while (i >= 0) && ((nth_char s i) != '.) do
          set i = i - 1;
      i
    );;
  • returns whether a string has only the characters of a pattern : fun [S S] I
fun stringIsPattern (s, pattern)=
    let 0 -> i in
    let 1 -> r in
    (
    while ((i < (strlen s)) && (r == 1)) do
        let strfind substr s i 1 pattern 0 -> p in
        if p == nil then
            set r = 0
        else
            set i = i+1;
    r
    );;

Author : iri
Date : december 2011

Return to Examples

Updated by iri about 13 years ago ยท 2 revisions