2
* This file is part of the SUB Commons project.
3
* Visit the websites for more information.
4
* - http://gdz.sub.uni-goettingen.de
6
* Copyright 2009, Center for Retrospective Digitization, Göttingen (GDZ),
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
13
* http://www.apache.org/licenses/LICENSE-2.0
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.
21
package de.unigoettingen.sub.commons.util;
23
import java.util.ArrayList;
24
import java.util.List;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
28
// TODO: Auto-generated Javadoc
30
* The Class StringUtils is a simple utility class for advanced (read obscure) string operations
32
public class StringUtils {
35
* Returns the index within the given String of the first occurrence of the specified regular expression.
37
* @param str the String to search
38
* @param searchStr the regular expression to search for.
40
* @return the index of the found expression
42
public static int indexOfRegex (String str, String searchStr) {
43
Pattern p = Pattern.compile(searchStr);
44
Matcher m = p.matcher(str);
52
* Returns List of indexes within the given String of all occurrences of the specified character.
57
* @return the List of indexes
59
public static List<Integer> allIndexOf (String s1, String s2) {
60
List<Integer> al = new ArrayList<Integer>();
61
StringBuffer sb = new StringBuffer(s1);
63
while (sb.indexOf(s2) >= 0) {
64
Integer pos = sb.indexOf(s2);
66
base += sb.delete(0, pos + s2.length()).length();
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) {
91
* Tests if the given string starts with the specified prefix or ends with the specified suffix.
93
* @param in the string to test
94
* @param str the pre- or suffix
96
* @return true, if successful
98
public static boolean startsOrEndsWith(String in, String str) {
99
if (in.startsWith(str)) {
102
if (in.endsWith(str)) {
109
* Tests if the given string ends with the specified suffix.
111
* @param in the string to test
112
* @param str the suffix
114
* @return true, if successful
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]))) {
130
* Escape a regular expression.
132
* @param str the string containing a regular expression
134
* @return the escaped string
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("\\]", "\\\\]");
152
* Returns true if and only if the given string contains the specified string, ignoring the case aof each string.
154
* @param in the string to test
155
* @param contains the string to search for
157
* @return true, if successful
159
public static boolean containsIgnoreCase (String in, String contains) {
160
return in.toLowerCase().contains(contains.toLowerCase());