Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. public class Test
  2. {
  3. public static void main( String[] args )
  4. {
  5. StringBuilder myName = new StringBuilder("AD-BJR5U");
  6. myName.setCharAt(3, '*');
  7. myName.setCharAt(4, '*');
  8. myName.setCharAt(5, '*');
  9. System.out.println(myName);
  10. }
  11. }
  12.  
  13. (?<=^(?:.{3}|.{4}|.{5})).
  14.  
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17.  
  18. final String regex = "(?<=^(?:.{3}|.{4}|.{5})).";
  19. final String string = "AD-BJR5U";
  20. final String subst = "*";
  21.  
  22. final Pattern pattern = Pattern.compile(regex);
  23. final Matcher matcher = pattern.matcher(string);
  24.  
  25. // The substituted value will be contained in the result variable
  26. final String result = matcher.replaceAll(subst);
  27.  
  28. System.out.println("Substitution result: " + result);
  29.  
  30. myName.replace(3, 5, "***");
  31.  
  32. // Keeping 3 firsts char; will replace the 3 of the center, and keep all to the end
  33. ^(.{3}).{3}(.*)$
  34.  
  35. str.replaceAll("^(.{3}).{3}(.*)$", "$1***$2");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement