Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b26831888801f84dfb8d6966084383a4765dbbe7 (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
/*******************************************************************************
 * Copyright (c) 2004, 2017 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.ui.internal.intro.impl.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class StringUtil {

    public static StringBuffer concat(String... strings) {
    	StringBuffer buffer = new StringBuffer();
    	for (String string : strings) {
			buffer.append(string);
		}
    	return buffer;
    }

	public static String decode(String s, String enc) throws UnsupportedEncodingException {
		try {
			return URLDecoder.decode(s, enc);
		}
		catch (Exception ex) {
			// fall back to original string
			return s;
		}
	}

    // Removes leading and trailing whitespace and replaces other
    // occurrences with a single space.

	public static String normalizeWhiteSpace(String input) {
		if (input == null) {
			return null;
		}
		StringBuffer result = new StringBuffer();
		boolean atStart = true;
		boolean whitespaceToInsert = false;
		for (int i = 0; i < input.length(); i++) {
			char next = input.charAt(i);
			if (Character.isWhitespace(next)) {
				if (!atStart) {
					whitespaceToInsert = true;
				}
			} else {
				if (whitespaceToInsert) {
					result.append(' ');
					whitespaceToInsert = false;
				}
				atStart = false;
				result.append(next);
			}
		}
		return result.toString();
	}

}

Back to the top