Guest User

Untitled

a guest
Sep 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1.  
  2. public class ByteTools
  3. {
  4. public static void main(String args[])
  5. {
  6. try
  7. {
  8. String s = new String("®");
  9. String s2 = "®A®®BC®DA®";
  10. String toRemove = "®";
  11.  
  12. String result = new String(replaceBytes(s2.getBytes("UTF8"), toRemove.getBytes("UTF8")), "UTF8");
  13. System.out.println("Result is : " + result.toString());
  14. }
  15. catch(Exception e)
  16. {
  17. e.printStackTrace();
  18. }
  19.  
  20.  
  21. }
  22.  
  23. private static byte[] replaceBytes(byte[] documentContent, byte[] toRemove)
  24. {
  25. int readIndex = 0;
  26. int writeIndex = 0;
  27.  
  28. while(readIndex < documentContent.length)
  29. {
  30. if(documentContent[readIndex] == toRemove[0])
  31. {
  32. if(matchFound(documentContent, toRemove, readIndex))
  33. {
  34. readIndex = readIndex + toRemove.length;
  35. }
  36. }
  37. else
  38. {
  39. documentContent[writeIndex++] = documentContent[readIndex++];
  40. }
  41. }
  42.  
  43. while(writeIndex < documentContent.length)
  44. documentContent[writeIndex++] = 0;
  45.  
  46. return documentContent;
  47. }
  48.  
  49. private static boolean matchFound(byte[] documentContent, byte[] toRemove, int startPosn)
  50. {
  51. int numBytesMatched = 0;
  52.  
  53. for(int i = 0; i < toRemove.length && startPosn + i < documentContent.length; i++)
  54. {
  55. if(documentContent[startPosn + i] == toRemove[i])
  56. numBytesMatched++;
  57. else
  58. break;
  59. }
  60.  
  61. return (numBytesMatched == toRemove.length) ? true : false;
  62. }
  63. }
Add Comment
Please, Sign In to add comment