1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.strings;
14 import java.text.DecimalFormat;
15 import java.text.NumberFormat;
16 import java.text.ParseException;
17 import java.util.Locale;
19 public class StringUtils {
21 private static NumberFormat usFormat = DecimalFormat.getInstance(Locale.US);
22 public final static String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$
25 * Ensures that a string is not null. Converts null strings into empty
26 * strings, and leaves any other string unmodified. Use this to help
27 * wrap calls to methods that return null instead of the empty string.
28 * Can also help protect against implementation errors in methods that
29 * are not supposed to return null.
31 * @param input input string (may be null)
32 * @return input if not null, or the empty string if input is null
34 public static String safeString(String input) {
39 return ZERO_LENGTH_STRING;
43 public static double safeDouble(String input) {
45 if(input == null) return Double.NaN;
49 String usafied = input.replace(",", ".");
50 Number number = usFormat.parse(usafied);
51 return number.doubleValue();
53 } catch (ParseException e) {
61 public static String repeat(String pattern, int repeatCount)
63 StringBuilder sb = new StringBuilder(pattern.length() * repeatCount);
64 for (int i=0; i<repeatCount; i++)