Java Regex String Replace First replaceLeading(String string, char replaced, char replacedBy)

Here you can find the source of replaceLeading(String string, char replaced, char replacedBy)

Description

replace Leading

License

Open Source License

Declaration

public static String replaceLeading(String string, char replaced, char replacedBy) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String replaceLeading(String string, char replaced, char replacedBy) {
        Pattern p = Pattern.compile(replaced + "++");
        Matcher matcher = p.matcher(string);
        if (matcher.find()) {
            string = matcher.replaceFirst(matcher.group(0).replace(replaced, replacedBy));
        }/*  w w w  .  ja  v  a 2s . c o m*/
        return string;
    }

    public static String replaceLeading(String string, String replaced, String replacedBy) {
        Pattern p = Pattern.compile("(" + replaced + ")++");
        Matcher matcher = p.matcher(string);
        if (matcher.find()) {
            string = matcher.replaceFirst(matcher.group(0).replace(replaced, replacedBy));
        }
        return string;
    }
}

Related

  1. replaceFirst(String str, String regex, String replacement, int patternFlag)
  2. replaceFirst(StringBuffer buf, StringBuffer aux, Pattern pattern, String replacement, int pos)
  3. replaceFirstIgnoreCase(String source, String search, String replace)
  4. replaceFirstOccurrence(String originalText, String oldString, String newString)
  5. replaceFirstStatement(String source, String regex, String statement)