Why is my Java string being padded with nulls?
<p>I have the following code, copy/paste from a method:</p>

<pre><code>String harmonicFile;
...
...
harmonicFile = String.format("%04d%s",this.number, this.suffix.toUpperCase()) + ".tch";
</code></pre>

<p>The result <strong><em>looks</em></strong> OK but I have some funkiness going on:</p>

<p>I pass this string to another method which includes this:</p>

<pre><code>if(filename.equals(file)){
   return file;
}
</code></pre>

<p>fileName is the value passed in and file is a match from the assets file system.  Suppose file is "0062.tch".  Inspecting filename in the Intellij IDEA debugger shows it to equal "0062.tch" yet the return statement is never hit.  On close inspection, I can see that filename is padded with 8 null chars (\u0000).  </p>

<p>I have 2 questions:</p>

<ol>
<li><p>Why is the string padded?</p></li>
<li><p>In IDEA, breaking on the if statement then using the expression evaluator (Alt-F8) shows filename.equals(file) to be true, but stepping through my code at runtime shows that it is not.  I assume that this is some low level difference in the way IDEA evaluates expressions and the Dalvik VM?  I assume that the VM is correct since filename does indeed NOT equal file. </p></li>
</ol>

<p>Thanks for any insights.</p>

<p>Simon</p>

<p>[EDIT]  I'm convinced that the rest of the code is not relevant but here it is anyway, comments removed to save space.</p>

<pre><code>public String filePathForHarmonicFile(){

    String harmonicFile;
    int number = this.number;

    if(this.number &lt; 0){ return "";}

    if(number&lt;10000 &amp;&amp; this.suffix.length()&lt;=1){
        harmonicFile = String.format("%04d%s",this.number, this.suffix.toUpperCase()) + ".tch";
    }else{
        harmonicFile = String.format("%s%s",this.number,this.suffix.toUpperCase()) + ".tch";
    }

    return SCFileManager.getAssetFilePath("",harmonicFile);
}

public static String getAssetFilePath(String rootOfSearch, String filename){

    try {

        // get a list of all file entries in the search root
        String[] files = ThisApplication.getContext().getAssets().list(rootOfSearch);

        for (String file:files){

            String newRoot;
            if (rootOfSearch.equals("")){
                newRoot = file;
            } else {
                newRoot = rootOfSearch + File.separator + file;
            }

            if (TidesPlannerApplication.getContext().getAssets().list(newRoot).length&gt;0){

                String thisFile = getAssetFilePath(newRoot,filename);

                if (!thisFile.equals("")){
                    return thisFile;
                }
            } else {
                if(file.equals(filename)){
                    return file;
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();  
    }

    return "";

}
</code></pre>

<p>I know this last method is butt ugly but if someone knows a better way to deal with 7.5k files in subfolders in assets, I'm all ears - AssetManager is $%^&amp;* brain dead!</p>
