Java Utililty Methods String Sanitize

List of utility methods to do String Sanitize

Description

The list of methods to do String Sanitize are organized into topic(s).

Method

StringsanitizePackageVersion(String string)
Makes a valid and useful package version string from one that cannot be used or is unsuitable.
return string.replaceAll("_", "");
StringsanitizePath(String apiPath)
This sanitizes an API path.
return apiPath.replaceAll("[: ]+.*?}", "}");
StringBuffersanitizePath(String input)
Sanitize the given filename/path, so that (1) it is not surrounded with quotation marks "", (2) all occurences of "\\" are changed to a "/", and (3) all occurences of "\" are changed to a "/".
StringBuffer buf = new StringBuffer();
if (input.matches("^\".+\"$")) {
    input = input.substring(1, input.length() - 1);
input = input.replaceAll("\\\\\\\\", "/");
input = input.replaceAll("\\\\", "/");
buf.append(input);
return buf;
...
ClasssanitizePrimitives(Class clazz)

This method checks if the input clazz can be converted to a primitive type.

if (clazz == Byte.class) {
    return byte.class;
if (clazz == Short.class) {
    return short.class;
if (clazz == Integer.class) {
    return int.class;
...
StringsanitizeProcOrFuncName(String src)
Next two functions are to help DBMD check if the given string is in form of database.name and return it as "database";"name" with comments removed.
if ((src == null) || (src.equals("%"))) {
    return null;
return src;
StringsanitizeProjectName(final String projectName)
sanitize Project Name
return projectName != null ? projectName.replaceAll("[^A-Za-z0-9_\\-.]", "") : projectName;
StringsanitizeRefName(String refName)
Strips the 'ref/someprefix/' prefix from a reference name if needed
if (refName.startsWith(REF_HEAD_PREFIX)) {
    refName = refName.substring(REF_HEAD_PREFIX.length());
if (refName.startsWith(REF_TAG_PREFIX)) {
    refName = refName.substring(REF_TAG_PREFIX.length());
return refName;
StringsanitizeResource(String resource)
sanitize Resource
String res = resource.trim();
if (res.startsWith("/")) {
    res = res.substring(1);
if (res.endsWith("/")) {
    res = res.substring(0, res.length() - 1);
return res;
...
StringsanitizeSAMHeader(String samString)
Takes a SAM string and strips out header fields that typically change from run to run.
return samString.replaceAll("@PG.*\n", "").replaceAll("@CO.*\n", "");
StringsanitizeSingleQuotesInAlbumItemValues(String value)
Transforms a value of an album Item and escapes single quotes.
int lastIndex = 0;
int singleQuoteIndex = value.indexOf('\'', 0);
StringBuilder sb = new StringBuilder();
while (singleQuoteIndex != -1) {
    sb.append(value.substring(lastIndex, singleQuoteIndex));
    sb.append("''");
    lastIndex = singleQuoteIndex + 1;
    singleQuoteIndex = value.indexOf('\'', singleQuoteIndex + 1);
...