简体   繁体   中英

Spaces not working as I think they should in jsp

I'm developing a web application in which I want to try to align the text vertically as much as possible and I want to do it by inserting spaces.

An example of the above would be having something like:

abifdbf wewe werow fowefj

abcd

wefew r fwerfwe wq

That would be left like this after using spaces:

abifdbf wewe werow-- fowefj

a--------b-------c----------d

wefew r--------fwerfwe wq

I know this is not a good practice, but right now we're just testing data and I think it should be easier to do this way than doing it by html design, specially considering that there's no definitive design still.

I've written this code, that I think should work:

 String correoyvisita = iniemail + " &nbsp" + primeravisita;

 int diferencialong = 58 - correoyvisita.length();

 for (int u = 0; u < diferencialong; u++)
 {
     correoyvisita=correoyvisita+"&nbsp;";
 }

Where correoyvisita and primeravisita are both strings, so I think that if their length is near to 58 then few spaces should be added ti it, if it's not more of then would be insserted and at the end they would have exactly a length of 58 characters.

But the same number of sapces seems always to be inserted and I cannot understand why.

Hope, you can guide me with this.

Solution

Well I was finally able of solving it using a non proportional font and the code I wrote on my question, I also tried to use an html table again but still did strange things... But whatever, now it works for testing purposes.

"&nbsp;&nbsp" is 11 characters length string.
correoyvisita.length(); counts these &nbsp as 11 characters but not 2 characters as they are displayed in browser.

Change your code a little:

String correoyvisita = iniemail + "  " + primeravisita;

int diferencialong= 58 - correoyvisita.length();

for (int u=0;u<diferencialong;u++)
{
    correoyvisita = correoyvisita + " ";
}
correoyvisita = correoyvisita.replace(" ", "&nbsp;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM