Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 903f6ceed7cf7e27748d768479714db782f19259 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.net;

import java.util.ArrayList;

public class StringUtil {

	/**
	 * Splits a string into substrings using a delimiter array.
	 *
	 * @param value
	 *            string to be tokenized
	 * @param delimiters
	 *            array of delimiters
	 * @return array of tokens
	 */
	public static String[] split(String value, String[] delimiters) {
		ArrayList result = new ArrayList();
		int firstIndex = 0;
		int separator = 0;
		while (firstIndex != -1) {
			firstIndex = -1;
			for (int i = 0; i < delimiters.length; i++) {
				int index = value.indexOf(delimiters[i]);
				if (index != -1 && (index < firstIndex || firstIndex == -1)) {
					firstIndex = index;
					separator = i;
				}
			}
			if (firstIndex != -1) {
				if (firstIndex != 0) {
					result.add(value.substring(0, firstIndex));
				}
				int newStart = firstIndex + delimiters[separator].length();
				if (newStart <= value.length()) {
					value = value.substring(newStart);
				}
			} else if (value.length() > 0) {
				result.add(value);
			}
		}
		return (String[]) result.toArray(new String[0]);
	}

	/**
	 * Tests equality of the given strings.
	 *
	 * @param sequence1
	 *            candidate 1, may be null
	 * @param sequence2
	 *            candidate 2, may be null
	 * @return true if both sequences are null or the sequences are equal
	 */
	public static final boolean equals(final CharSequence sequence1,
			final CharSequence sequence2) {
		if (sequence1 == sequence2) {
			return true;
		} else if (sequence1 == null || sequence2 == null) {
			return false;
		}
		return sequence1.equals(sequence2);
	}

	/**
	 * Replace within <code>source</code> the occurrences of <code>from</code>
	 * with <code>to</code>.<br>
	 * <b>Note:</b> This has the same behavior as the
	 * <code>String.replace()</code> method within JDK 1.5.
	 *
	 * @param source
	 * @param from
	 * @param to
	 * @return the substituted string
	 */
	public static String replace(String source, String from, String to) {
		if (from.length() == 0)
			return source;
		StringBuffer buffer = new StringBuffer();
		int current = 0;
		int pos = 0;
		while (pos != -1) {
			pos = source.indexOf(from, current);
			if (pos == -1) {
				buffer.append(source.substring(current));
			} else {
				buffer.append(source.substring(current, pos));
				buffer.append(to);
				current = pos + from.length();
			}
		}
		return buffer.toString();
	}

	public static boolean hostMatchesFilter(String host, String filter) {
		String suffixMatchingFilter = "*" + filter; //$NON-NLS-1$
		StringMatcher matcher = new StringMatcher(suffixMatchingFilter, true, false);
		return matcher.match(host);
	}

}

Back to the top