1 | using System; |
---|
2 | |
---|
3 | namespace antlr |
---|
4 | { |
---|
5 | /* ANTLR Translator Generator |
---|
6 | * Project led by Terence Parr at http://www.jGuru.com |
---|
7 | * Software rights: http://www.antlr.org/license.html |
---|
8 | * |
---|
9 | * $Id:$ |
---|
10 | */ |
---|
11 | // |
---|
12 | // ANTLR C# Code Generator by Micheal Jordan |
---|
13 | // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com |
---|
14 | // Anthony Oguntimehin |
---|
15 | // |
---|
16 | // With many thanks to Eric V. Smith from the ANTLR list. |
---|
17 | // |
---|
18 | |
---|
19 | public class StringUtils |
---|
20 | { |
---|
21 | /*General-purpose utility function for removing |
---|
22 | * characters from back of string |
---|
23 | * @param s The string to process |
---|
24 | * @param c The character to remove |
---|
25 | * @return The resulting string |
---|
26 | */ |
---|
27 | static public string stripBack(string s, char c) |
---|
28 | { |
---|
29 | while (s.Length > 0 && s[s.Length - 1] == c) |
---|
30 | { |
---|
31 | s = s.Substring(0, (s.Length - 1) - (0)); |
---|
32 | } |
---|
33 | return s; |
---|
34 | } |
---|
35 | |
---|
36 | /*General-purpose utility function for removing |
---|
37 | * characters from back of string |
---|
38 | * @param s The string to process |
---|
39 | * @param remove A string containing the set of characters to remove |
---|
40 | * @return The resulting string |
---|
41 | */ |
---|
42 | static public string stripBack(string s, string remove) |
---|
43 | { |
---|
44 | bool changed; |
---|
45 | do |
---|
46 | { |
---|
47 | changed = false; |
---|
48 | for (int i = 0; i < remove.Length; i++) |
---|
49 | { |
---|
50 | char c = remove[i]; |
---|
51 | while (s.Length > 0 && s[s.Length - 1] == c) |
---|
52 | { |
---|
53 | changed = true; |
---|
54 | s = s.Substring(0, (s.Length - 1) - (0)); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | while (changed); |
---|
59 | return s; |
---|
60 | } |
---|
61 | |
---|
62 | /*General-purpose utility function for removing |
---|
63 | * characters from front of string |
---|
64 | * @param s The string to process |
---|
65 | * @param c The character to remove |
---|
66 | * @return The resulting string |
---|
67 | */ |
---|
68 | static public string stripFront(string s, char c) |
---|
69 | { |
---|
70 | while (s.Length > 0 && s[0] == c) |
---|
71 | { |
---|
72 | s = s.Substring(1); |
---|
73 | } |
---|
74 | return s; |
---|
75 | } |
---|
76 | |
---|
77 | /*General-purpose utility function for removing |
---|
78 | * characters from front of string |
---|
79 | * @param s The string to process |
---|
80 | * @param remove A string containing the set of characters to remove |
---|
81 | * @return The resulting string |
---|
82 | */ |
---|
83 | static public string stripFront(string s, string remove) |
---|
84 | { |
---|
85 | bool changed; |
---|
86 | do |
---|
87 | { |
---|
88 | changed = false; |
---|
89 | for (int i = 0; i < remove.Length; i++) |
---|
90 | { |
---|
91 | char c = remove[i]; |
---|
92 | while (s.Length > 0 && s[0] == c) |
---|
93 | { |
---|
94 | changed = true; |
---|
95 | s = s.Substring(1); |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | while (changed); |
---|
100 | return s; |
---|
101 | } |
---|
102 | |
---|
103 | /*General-purpose utility function for removing |
---|
104 | * characters from the front and back of string |
---|
105 | * @param s The string to process |
---|
106 | * @param head exact string to strip from head |
---|
107 | * @param tail exact string to strip from tail |
---|
108 | * @return The resulting string |
---|
109 | */ |
---|
110 | public static string stripFrontBack(string src, string head, string tail) |
---|
111 | { |
---|
112 | int h = src.IndexOf(head); |
---|
113 | int t = src.LastIndexOf(tail); |
---|
114 | if (h == - 1 || t == - 1) |
---|
115 | return src; |
---|
116 | return src.Substring(h + 1, (t) - (h + 1)); |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|