daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Sep 20th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top