What is the best solution to appen a line at the beginning of a String variable in Java? -


i have following situation.

into java application have string variable named *fattura** contains structured data extracted xml...so fattura variable contains text this:

<fatturaelettronicabody>                                                             <datigenerali>                                                                           <datigeneralidocumento>                                                                      <tipodocumento>td01</tipodocumento>                                                          <divisa>eur</divisa>                                                                         <data>2015-03-16</data>                                                                      <numero>004600002117</numero>                                                                <importototaledocumento>9.57</importototaledocumento>                                    </datigeneralidocumento>                                                             </datigenerali> </fatturaelettronicabody> 

now need append @ beginning of previous xml code line identify xml section xml document itself, one:

<?xml version="1.0" encoding="utf-8"?>  

so final result have this:

<?xml version="1.0" encoding="utf-8"?>    <fatturaelettronicabody>                                                                 <datigenerali>                                                                               <datigeneralidocumento>                                                                          <tipodocumento>td01</tipodocumento>                                                              <divisa>eur</divisa>                                                                             <data>2015-03-16</data>                                                                          <numero>004600002117</numero>                                                                    <importototaledocumento>9.57</importototaledocumento>                                        </datigeneralidocumento>                                                                 </datigenerali> </fatturaelettronicabody>  

so done creating new string fatturaxml variable , first append line it:

    <?xml version="1.0" encoding="utf-8"?> 

and append fattura variable content.

but exist smarter solution? have fattura variable contains xml content, can directly put line before content of fattura variable without passing new variable?

it need work on string, there simple string prepend options:

fattura  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + fattura ; 

i guess mentioned that.

but think stringbuilder better way string manipulation operations strings immutable in java. if you're manipulating string more once, otherwise might overkill.

stringbuilder _fatturabuilder = new stringbuilder(fattura); _fatturabuilder.insert(0, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -