Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test
- {
- public static void main( String[] args )
- {
- StringBuilder myName = new StringBuilder("AD-BJR5U");
- myName.setCharAt(3, '*');
- myName.setCharAt(4, '*');
- myName.setCharAt(5, '*');
- System.out.println(myName);
- }
- }
- (?<=^(?:.{3}|.{4}|.{5})).
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- final String regex = "(?<=^(?:.{3}|.{4}|.{5})).";
- final String string = "AD-BJR5U";
- final String subst = "*";
- final Pattern pattern = Pattern.compile(regex);
- final Matcher matcher = pattern.matcher(string);
- // The substituted value will be contained in the result variable
- final String result = matcher.replaceAll(subst);
- System.out.println("Substitution result: " + result);
- myName.replace(3, 5, "***");
- // Keeping 3 firsts char; will replace the 3 of the center, and keep all to the end
- ^(.{3}).{3}(.*)$
- str.replaceAll("^(.{3}).{3}(.*)$", "$1***$2");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement