java - How to append a string into a front of an original text to make new text & then trip out the front string without affect the original text? -


ok, here problem. have need format text based on provided info. constraint, can append control string front of original text & when processing have trip out front string original text. not text needs controlstring.

ex:

string originaltext1="iphone"; string controlstring= "colorlevel=2*"; string newtext1 =controlstring+originaltext1; //ie newtext=colorlevel=2*iphone  string originaltext2="iphone 3g"; // originaltext2 has no controlstring string newtext2="iphone 3g";  string originaltext3="colorlevel=2*aaa"; // has no controlstring front same controlstring string newtext3="colorlevel=2*aaa";  string originaltext4="colorlevel=2*bbb"; // has controlstring @ same time front same controlstring string controlstring= "colorlevel=2*"; string newtext4 =controlstring+originaltext4; // ie newtext4=colorlevel=2*colorlevel=2*bbb 

ok, need function read these new text. function need trip off controlstring original.

so question is, how encode original text in such way when trip out front string of new text won't affect original text?

if don't prefer constant control string. can build control string based on input.

static final string delimiterchar = "~"; static final string controlstringpattern = "^~-?\\d+~";  public static stringbuffer  encode(string orgtext){     stringbuffer buffer = new stringbuffer();     buffer.append(delimiterchar);     buffer.append(orgtext.hashcode());     buffer.append(delimiterchar);     buffer.append(orgtext);     return buffer;  } 

here have added "~" before , after hashcode given input string. note** , string's hashcode method, go through characters in string , perform multiplication of 31

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 

so based on given string. , may impact performance , if planning use huge string org string. , hashcode built once life time object.

you can replace hashcode string think unquie.

to decode

public static string  decode(string orgtext){     return  orgtext.replaceall(controlstringpattern, ""); } 

more detailed decode . make sure hashcode given number should match decoded string. double check

public static string  decode(string orgtext){     matcher matcher = pattern.matcher(orgtext);     if (matcher.find()){         string hashstring = matcher.group();         int hash = integer.parseint(hashstring.substring(1,           hashstring.length()-1));         string txt = orgtext.substring(hashstring.length());         if (txt.hashcode() == hash){             return txt;         }     }     return orgtext; } 

to perform test

public static void main(string[] args) {     //system.out.println(new string("~12345~").matches(controlstringpattern));     string orgtext = "hi org text";     stringbuffer encodetext = encode(orgtext);     system.out.println("orginial :" + orgtext);     system.out.println("encoded :" + encodetext);     system.out.println("decoded :" + decode(encodetext.tostring()));     system.out.println("decoded :" + decode("i not encoded"));  } 

and output

orginial :hi org text encoded :~-1828588409~hi org text decoded :hi org text decoded :i not encoded 

if want control string alone..

public static string getcontrolstringifexists(string orgtext){     matcher matcher = pattern.matcher(orgtext);     if (matcher.find()){         string hashstring = matcher.group();         int hash = integer.parseint(hashstring.substring(1, hashstring.length()-1));         string txt = orgtext.substring(hashstring.length());         if (txt.hashcode() == hash){             return hashstring;         }     }     return null; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -