~slub.team/goobi-production/bug-1013622

« back to all changes in this revision

Viewing changes to src/de/unigoettingen/sub/commons/util/StringUtils.java

  • Committer: Ralf Claussnitzer
  • Date: 2012-05-29 10:55:34 UTC
  • mfrom: (66.1.1 integrate-util)
  • Revision ID: ralf.claussnitzer@slub-dresden.de-20120529105534-t5u5vxj9x5v2vifb
fixes lp:1005844 integrate sub-commons util library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the SUB Commons project.
 
3
 * Visit the websites for more information. 
 
4
 *              - http://gdz.sub.uni-goettingen.de 
 
5
 * 
 
6
 * Copyright 2009, Center for Retrospective Digitization, Göttingen (GDZ),
 
7
 * 
 
8
 * 
 
9
 * Licensed under the Apache License, Version 2.0 (the “License”);
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 * 
 
13
 *  http://www.apache.org/licenses/LICENSE-2.0
 
14
 * 
 
15
 * Unless required by applicable law or agreed to in writing, software
 
16
 * distributed under the License is distributed on an “AS IS” BASIS,
 
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
18
 * See the License for the specific language governing permissions and
 
19
 * limitations under the License.
 
20
 */
 
21
package de.unigoettingen.sub.commons.util;
 
22
 
 
23
import java.util.ArrayList;
 
24
import java.util.List;
 
25
import java.util.regex.Matcher;
 
26
import java.util.regex.Pattern;
 
27
 
 
28
// TODO: Auto-generated Javadoc
 
29
/**
 
30
 * The Class StringUtils is a simple utility class for advanced (read obscure) string operations
 
31
 */
 
32
public class StringUtils {
 
33
 
 
34
        /**
 
35
         * Returns the index within the given String of the first occurrence of the specified regular expression.
 
36
         * 
 
37
         * @param str the String to search
 
38
         * @param searchStr the regular expression to search for.
 
39
         * 
 
40
         * @return the index of the found expression
 
41
         */
 
42
        public static int indexOfRegex (String str, String searchStr) {
 
43
                Pattern p = Pattern.compile(searchStr);
 
44
                Matcher m = p.matcher(str);
 
45
                if (m.find()) {
 
46
                        return m.start();       
 
47
                }
 
48
                return -1;
 
49
        }
 
50
 
 
51
        /**
 
52
         * Returns List of indexes within the given String of all occurrences of the specified character.
 
53
         * 
 
54
         * @param s1 the s1
 
55
         * @param s2 the s2
 
56
         * 
 
57
         * @return the List of indexes
 
58
         */
 
59
        public static List<Integer> allIndexOf (String s1, String s2) {
 
60
                List<Integer> al = new ArrayList<Integer>();
 
61
                StringBuffer sb = new StringBuffer(s1);
 
62
                Integer base = 0;
 
63
                while (sb.indexOf(s2) >= 0) {
 
64
                        Integer pos = sb.indexOf(s2);
 
65
                        al.add(pos + base);
 
66
                        base += sb.delete(0, pos + s2.length()).length();
 
67
                }
 
68
                return al;
 
69
        }
 
70
        
 
71
        /**
 
72
         * Index of occurance.
 
73
         * 
 
74
         * @param s1 the s1
 
75
         * @param s2 the s2
 
76
         * @param i the i
 
77
         * 
 
78
         * @return the int
 
79
         */
 
80
        public static int indexOfOccurance (String s1, String s2, Integer i) {
 
81
                ArrayList<Integer> al = new ArrayList<Integer>();
 
82
                al.addAll(allIndexOf(s1, s2));
 
83
                if (al.size() <= i - 1) {
 
84
                        return al.get(i - i);
 
85
                } else {
 
86
                        return -1;
 
87
                }
 
88
        }
 
89
        
 
90
        /**
 
91
         * Tests if the given string starts with the specified prefix or ends with the specified suffix.
 
92
         * 
 
93
         * @param in the string to test
 
94
         * @param str the pre- or suffix
 
95
         * 
 
96
         * @return true, if successful
 
97
         */
 
98
        public static boolean startsOrEndsWith(String in, String str) {
 
99
                if (in.startsWith(str)) {
 
100
                        return true;
 
101
                }
 
102
                if (in.endsWith(str)) {
 
103
                        return true;
 
104
                }
 
105
                return false;
 
106
        }
 
107
 
 
108
        /**
 
109
         * Tests if the given string ends with the specified suffix.
 
110
         * 
 
111
         * @param in the string to test
 
112
         * @param str the suffix
 
113
         * 
 
114
         * @return true, if successful
 
115
         * 
 
116
         * @deprecated
 
117
         */
 
118
        @Deprecated
 
119
        public static boolean endsWith(String in, String str) {
 
120
                char[] c = str.toCharArray();
 
121
                for (int i = 0; i < c.length; i++) {
 
122
                        if (in.endsWith(String.valueOf(c[i]))) {
 
123
                                return true;
 
124
                        }
 
125
                }
 
126
                return false;
 
127
        }
 
128
 
 
129
        /**
 
130
         * Escape a regular expression.
 
131
         * 
 
132
         * @param str the string containing a regular expression
 
133
         * 
 
134
         * @return the escaped string
 
135
         */
 
136
        public static String escapeRegex (String str) {
 
137
                str = str.replaceAll("\\*", "\\\\*");
 
138
                str = str.replaceAll("\\.", "\\\\.");
 
139
                str = str.replaceAll("\\?", "\\\\?");
 
140
                str = str.replaceAll("\\+", "\\\\+");
 
141
                //str = str.replaceAll("\\", "\\\\");
 
142
                str = str.replaceAll("\\$", "\\\\$");
 
143
                str = str.replaceAll("\\|", "\\\\|");
 
144
                str = str.replaceAll("\\{", "\\\\{");
 
145
                str = str.replaceAll("\\}", "\\\\}");
 
146
                str = str.replaceAll("\\[", "\\\\[");
 
147
                str = str.replaceAll("\\]", "\\\\]");
 
148
                return str;
 
149
        }
 
150
        
 
151
        /**
 
152
         * Returns true if and only if the given string contains the specified string, ignoring the case aof each string.
 
153
         * 
 
154
         * @param in the string to test
 
155
         * @param contains the string to search for
 
156
         * 
 
157
         * @return true, if successful
 
158
         */
 
159
        public static boolean containsIgnoreCase (String in, String contains) {
 
160
                return in.toLowerCase().contains(contains.toLowerCase());
 
161
                
 
162
        }
 
163
        
 
164
}