Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ByteTools
- {
- public static void main(String args[])
- {
- try
- {
- String s = new String("®");
- String s2 = "®A®®BC®DA®";
- String toRemove = "®";
- String result = new String(replaceBytes(s2.getBytes("UTF8"), toRemove.getBytes("UTF8")), "UTF8");
- System.out.println("Result is : " + result.toString());
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- private static byte[] replaceBytes(byte[] documentContent, byte[] toRemove)
- {
- int readIndex = 0;
- int writeIndex = 0;
- while(readIndex < documentContent.length)
- {
- if(documentContent[readIndex] == toRemove[0])
- {
- if(matchFound(documentContent, toRemove, readIndex))
- {
- readIndex = readIndex + toRemove.length;
- }
- }
- else
- {
- documentContent[writeIndex++] = documentContent[readIndex++];
- }
- }
- while(writeIndex < documentContent.length)
- documentContent[writeIndex++] = 0;
- return documentContent;
- }
- private static boolean matchFound(byte[] documentContent, byte[] toRemove, int startPosn)
- {
- int numBytesMatched = 0;
- for(int i = 0; i < toRemove.length && startPosn + i < documentContent.length; i++)
- {
- if(documentContent[startPosn + i] == toRemove[i])
- numBytesMatched++;
- else
- break;
- }
- return (numBytesMatched == toRemove.length) ? true : false;
- }
- }
Add Comment
Please, Sign In to add comment