Useful string » History » Version 2
iri, 12/17/2011 10:33 PM
1 | 1 | iri | h1. Useful string functions |
---|---|---|---|
2 | |||
3 | * Remove the last character of a string : |
||
4 | |||
5 | <pre> |
||
6 | 2 | iri | fun removeLastChar (s)= |
7 | 1 | iri | substr s 0 (strlen s)-1;; |
8 | </pre> |
||
9 | |||
10 | * Compare the last character. Return 1 if it is the same. |
||
11 | |||
12 | <pre> |
||
13 | 2 | iri | fun testLastChar (s, c)= |
14 | 1 | iri | let substr s (strlen s)-1 1 -> ch in |
15 | !strcmp c ch;; |
||
16 | </pre> |
||
17 | |||
18 | * Return the position of the last occurence of a character |
||
19 | |||
20 | <pre> |
||
21 | 2 | iri | fun getLastPosChar (string, char)= |
22 | 1 | iri | let 0 -> i in |
23 | let (strfind char string 0) + 1 -> npos in |
||
24 | while npos != nil do |
||
25 | ( |
||
26 | set i = npos; |
||
27 | set npos = (strfind char string npos) + 1; |
||
28 | i |
||
29 | );; |
||
30 | </pre> |
||
31 | |||
32 | * Return the position of the last / |
||
33 | |||
34 | <pre> |
||
35 | 2 | iri | fun getLastPosSlash (s)= |
36 | 1 | iri | if s == nil then nil else |
37 | let strlen s -> n in |
||
38 | let n-1 -> i in |
||
39 | ( |
||
40 | while (i >=0) && ((nth_char s i) != '/) do |
||
41 | set i = i-1; |
||
42 | i |
||
43 | );; |
||
44 | </pre> |
||
45 | |||
46 | * Return the position of the last . |
||
47 | |||
48 | <pre> |
||
49 | 2 | iri | fun getLastPosPoint (s)= |
50 | 1 | iri | if s == nil then nil else |
51 | let (strlen s) - 1 -> i in |
||
52 | ( |
||
53 | while (i >= 0) && ((nth_char s i) != '.) do |
||
54 | set i = i - 1; |
||
55 | i |
||
56 | );; |
||
57 | </pre> |
||
58 | |||
59 | * returns whether a string has only the characters of a pattern : fun [S S] I |
||
60 | |||
61 | <pre> |
||
62 | 2 | iri | fun stringIsPattern (s, pattern)= |
63 | 1 | iri | let 0 -> i in |
64 | let 1 -> r in |
||
65 | ( |
||
66 | while ((i < (strlen s)) && (r == 1)) do |
||
67 | let strfind substr s i 1 pattern 0 -> p in |
||
68 | if p == nil then |
||
69 | set r = 0 |
||
70 | else |
||
71 | set i = i+1; |
||
72 | r |
||
73 | );; |
||
74 | </pre> |
||
75 | |||
76 | Author : iri |
||
77 | Date : december 2011 |
||
78 | |||
79 | *Return to [[Examples]]* |