Example usage for java.lang String replaceFirst

List of usage examples for java.lang String replaceFirst

Introduction

In this page you can find the example usage for java.lang String replaceFirst.

Prototype

public String replaceFirst(String regex, String replacement) 

Source Link

Document

Replaces the first substring of this string that matches the given regular expression with the given replacement.

Usage

From source file:jp.furplag.util.commons.StringUtils.java

/**
 * Shorthand for {@code java.lang.String.replaceFirst(regex, "")}.
 *
 * @param str the string, may be null./*w  w w  .  j a  v  a2  s . c  o m*/
 * @param regex the regular expression to which this string is to be matched.
 * @return the resulting String.
 */
public static String truncateFirst(final String str, final String regex) {
    if (isSimilarToBlank(str))
        return str;
    if (isBlank(regex))
        return str;

    return str.replaceFirst(regex, EMPTY);
}

From source file:Main.java

private static String getNodeAttribute(Element element, String nodeWeiZhi, String attributeName) {
    String result = "";
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                result += "," + node.getAttributes().getNamedItem(attributeName).getNodeValue().trim();
            }//from   w w w  .j av  a2  s . co  m
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeAttribute((Element) node, nodeWeiZhiTemp, attributeName);
            }
        }
    }
    return result;
}

From source file:net.sf.sahi.util.Utils.java

public static String stripChildSessionId(final String sessionId) {
    return sessionId.replaceFirst("sahix[^x]+x", "");
}

From source file:gov.nih.nci.cananolab.util.StringUtils.java

/**
 * Convert a string with multiple words separated by space to one word, with
 * first letter as lower case.//from  w  ww  . j  a v a  2 s  . c  om
 *
 * @param words
 * @return
 */
public static String getOneWordLowerCaseFirstLetter(String words) {
    // remove space in words and make the first letter lower case.
    String oneWord = words;
    if (!isEmpty(words)) {
        String firstLetter = words.substring(0, 1);
        oneWord = words.replaceFirst(firstLetter, firstLetter.toLowerCase()).replace(" ", "");
    }
    return oneWord;
}

From source file:Main.java

public static void showChangelog(final Context context, final String title, final String appname,
        final int resChanges, final int resNotes) {
    final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    final String v0 = p.getString(PREFS_LAST_RUN, "");
    String v1 = null;/*from   ww w . ja  va2s .c o m*/
    try {
        v1 = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        Log.e(TAG, "package not found: " + context.getPackageName(), e);
    }
    p.edit().putString(PREFS_LAST_RUN, v1).commit();
    if (v0.length() == 0) {
        Log.d(TAG, "first boot, skip changelog");
        // return;
    }
    if (v0.equals(v1)) {
        Log.d(TAG, "no changes");
        return;
    }

    String[] changes = context.getResources().getStringArray(resChanges);
    String[] notes = resNotes > 0 ? context.getResources().getStringArray(resNotes) : null;

    final SpannableStringBuilder sb = new SpannableStringBuilder();
    for (String s : notes) {
        SpannableString ss = new SpannableString(s + "\n");
        int j = s.indexOf(":");
        if (j > 0) {
            if (!TextUtils.isEmpty(s)) {
                ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        sb.append(ss);
        sb.append("\n");
    }
    if (notes != null && notes.length > 0) {
        sb.append("\n");
    }
    for (String s : changes) {
        s = appname + " " + s.replaceFirst(": ", ":\n* ").replaceAll(", ", "\n* ") + "\n";
        SpannableString ss = new SpannableString(s);
        int j = s.indexOf(":");
        if (j > 0) {
            ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        sb.append(ss);
        sb.append("\n");
    }
    sb.setSpan(new RelativeSizeSpan(0.75f), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    changes = null;
    notes = null;

    AlertDialog.Builder b = new AlertDialog.Builder(context);
    b.setTitle(title);
    b.setMessage(sb);
    b.setCancelable(true);
    b.setPositiveButton(android.R.string.ok, null);
    b.show();
}

From source file:com.basho.riak.client.http.util.ClientUtils.java

/**
 * Return just the path portion of the given URL
 *//*from  www.  java 2 s  .  c o  m*/
public static String getPathFromUrl(String url) {
    if (url == null)
        return null;
    return url.replaceFirst(URL_PATH_MASK, "");
}

From source file:de.uniwue.info6.misc.StringTools.java

/**
 *
 *
 * @param path/*from   w  w  w  .j  ava 2 s  .co  m*/
 * @return
 */
public static String shortenUnixHomePath(String path) {
    if (path.startsWith(SystemUtils.USER_HOME) && (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC)) {
        path = path.replaceFirst(SystemUtils.USER_HOME, "~");
    }
    return path;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.migration.MigrationStepFrom025To026.java

public final static String getNewLocation(final String oldLocation) {
    final int index = oldLocation.indexOf(OBSERVABLE_TYPE_PREFIX);
    if (index < 0) {
        StringBuilder builder = new StringBuilder();
        builder.append("WARNING: ");
        builder.append("No ");
        builder.append(OBSERVABLE_TYPE_PREFIX);
        builder.append(" found in external data location '");
        builder.append(oldLocation);/*from  www  .ja  v a2  s .  c om*/
        builder.append("'.");
        operationLog.warn(builder.toString());
        return oldLocation;
    }
    return oldLocation.replaceFirst(OBSERVABLE_TYPE_PREFIX, DATA_SET_TYPE_PREFIX);
}

From source file:jp.furplag.util.commons.StringUtils.java

/**
 * replaces the last substring of this string that matches the given regular expression with the given replacement.
 *
 * @param str the string, may be null./*from w  w  w  .j ava  2  s  .com*/
 * @param regex the regular expression to which this string is to be matched.
 * @param replacement the string to be substituted for each match.
 * @return the resulting String.
 */
public static String replaceLast(final String str, final String regex, final String replacement) {
    if (isSimilarToBlank(str))
        return str;
    if (isBlank(regex))
        return str;

    return str.replaceFirst("(?s)(.*)" + defaultString(regex), "$1" + defaultString(replacement));
}

From source file:com.ibm.rpe.web.service.docgen.impl.TemplateBuilderImpl.java

private static String replacePlaceHolders(Map<String, String> params, String xmlPath) throws IOException {

    String cellFormat = readTemplatsFile(xmlPath);

    // params.put(ELEMENT_ID, Integer.toString(++START_ELEMENT_ID));

    // Use the original cell format string and replace the parameters
    for (Entry<String, String> entry : params.entrySet()) {
        cellFormat = cellFormat.replace(entry.getKey(), entry.getValue());
    }//from  w w  w .j  a  va2  s .  c  om

    while (cellFormat.contains(TemplateConstants.ELEMENT_ID)) {
        cellFormat = cellFormat.replaceFirst(TemplateConstants.ELEMENT_ID,
                Integer.toString(++START_ELEMENT_ID));
    }

    return cellFormat;
}