Advertisement
Guest User

Untitled

a guest
May 24th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // beta
  2. // Lisence Under GPL2
  3. // Author: S.Mahbub-Uz-Zaman
  4.  
  5. public static String a = "test,test,test";
  6. public static String replaceWith = " and ";
  7.  
  8. // using StringBuffer's replace method
  9. public static void replaceLastCommaSB () {
  10. StringBuffer sb = new StringBuffer(a);
  11. sb.replace(a.lastIndexOf(","), a.lastIndexOf(",") + 1, replaceWith);
  12. System.out.println(sb);
  13. }
  14.  
  15. output: test,test and test
  16.  
  17. // using String's substring method
  18. public static void replaceLastCommaSS () {
  19. String temp = a.substring(0, a.lastIndexOf(",")) + replaceWith + a.substring(a.lastIndexOf(",") + 1);
  20. System.out.println(temp);
  21. }
  22.  
  23.  
  24. output: test,test and test
  25.  
  26. // regular expression [work on progress]
  27. public static void replaceLastCommaRE () {
  28. String temp = a.replaceFirst(",", replaceWith);
  29. System.out.println(temp);
  30. }
  31.  
  32. output: test and test,test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement