All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kingsoft.services.util.StringUtils Maven / Gradle / Ivy

package com.kingsoft.services.util;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.codec.binary.Base64;

/**
 * Utilities for converting objects to strings.
 */
public class StringUtils {

  private static final String DEFAULT_ENCODING = "UTF-8";

  public static final String COMMA_SEPARATOR = ",";
  
  public static final String VERSION = "2016-03-01";
  
  public static final Charset UTF8 = Charset.forName(DEFAULT_ENCODING);

  public static String replace(String originalString, String partToMatch,
      String replacement) {
    StringBuilder buffer = new StringBuilder(originalString.length());
    buffer.append(originalString);

    int indexOf = buffer.indexOf(partToMatch);
    while (indexOf != -1) {
      buffer = buffer.replace(indexOf, indexOf + partToMatch.length(),
          replacement);
      indexOf = buffer.indexOf(partToMatch);
    }

    return buffer.toString();
  }

  /**
   * Joins the strings in parts with joiner between each string
   * 
   * @param joiner
   *          the string to insert between the strings in parts
   * @param parts
   *          the parts to join
   */
  public static String join(String joiner, String... parts) {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
      builder.append(parts[i]);
      if (i < parts.length - 1) {
        builder.append(joiner);
      }
    }
    return builder.toString();
  }

  /**
   * @return true if the given value is either null or the empty string
   */
  public static boolean isNullOrEmpty(String value) {
    if (value == null) {
      return true;
    }
    return value.isEmpty();
  }

  public static String base64Encode(byte[] data) {
    return Base64.encodeBase64String(data);
  }

  public static byte[] base64Decode(String data) throws IOException {
    return Base64.decodeBase64(data);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy