Bobby
Bobby

Reputation: 659

format a string with fill places

I have setup an edittext box and set the maxlength to 10. When I copy the edittext to a string myTitles. I need the myTiles to be 10 chars long and not dependent on what is entered in the edittext box.

      myTitles[0] = TitlesEdit.getText().toString();

The edittext was filled with ABCD so I need to add 6 spaces or placeholders after the ABCD. I have seen other post with str_pad and substr without success

        myTitles[0] = str_pad(strTest, 0, 10);
        myTitles[0] = substr(strTest,0, 10);

Upvotes: 1

Views: 44

Answers (3)

Bobby
Bobby

Reputation: 659

Thank you Lal, I use " " to fill and it worked fine. here is my new code.

                       String strTest = TitlesEdit.getText().toString();

                        for (int i = strTest.length(); i <= 10; i++) {
                            strTest += " ";
                        }

                        Log.d("TAG", "String" + strTest);
                        myTitles[intLinenumber] = strTest;

Upvotes: 0

Deepak Goyal
Deepak Goyal

Reputation: 4907

String s = new String("abcde");
for(int i=s.length();i<10;i++){
    s = s.concat("-");
}

Then output your string s.

Upvotes: 0

Lal
Lal

Reputation: 14810

Try something like

public static String newString(String str) {
    for (int i = str.length(); i <= 10; i++)
        str += "*";
    return str;
}

This will return a String with * replaced for the empty ones.

So, for eg, if your String is abcde, then on calling newString() as below

myTitles[0] = newString("abcde");

will return abcde***** as the output.

Upvotes: 4

Related Questions