怎么把txt里头的数据(以空格隔开)进行左对齐处理
我用的办法是如下:
public static String printFormattedFunds(Fund funds) {
String tmpStr = "";
String printStr =funds.getFundNumber()+" ";
if (funds.getFundName().trim().length() <30) {
tmpStr = funds.getFundName().trim();
for (int i = 0; i < (30 - funds.getFundName()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getFundName().trim();
}
printStr += tmpStr;
if (funds.getNetValueUnit().trim().length() <10) {
tmpStr = funds.getNetValueUnit().trim();
for (int i = 0; i < (10 - funds.getNetValueUnit()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetValueUnit().trim();
}
printStr += tmpStr;
if (funds.getNetValueAsset().trim().length() <10) {
tmpStr = funds.getNetValueAsset().trim();
for (int i = 0; i < (10 - funds.getNetValueAsset()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetValueAsset().trim();
}
printStr += tmpStr;
if (funds.getNetTotalValue().trim().length() <10) {
tmpStr = funds.getNetTotalValue().trim();
for (int i = 0; i < (10 - funds.getNetTotalValue()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetTotalValue().trim();
}
printStr += tmpStr;
if (funds.getNetRate().trim().length() <10) {
tmpStr = funds.getNetRate().trim();
for (int i = 0; i < (10 - funds.getNetRate()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetRate().trim();
}
printStr += tmpStr;
if (funds.getFundState().trim().length() <20) {
tmpStr = funds.getFundState().trim();
for (int i = 0; i < (20 - funds.getFundState()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getFundState().trim();
}
printStr += tmpStr;
printStr+=funds.getDateRelease();
return printStr;
}
但是如果字符串里面是中文的话就不行了,求解!!!
我用的办法是如下:
public static String printFormattedFunds(Fund funds) {
String tmpStr = "";
String printStr =funds.getFundNumber()+" ";
if (funds.getFundName().trim().length() <30) {
tmpStr = funds.getFundName().trim();
for (int i = 0; i < (30 - funds.getFundName()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getFundName().trim();
}
printStr += tmpStr;
if (funds.getNetValueUnit().trim().length() <10) {
tmpStr = funds.getNetValueUnit().trim();
for (int i = 0; i < (10 - funds.getNetValueUnit()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetValueUnit().trim();
}
printStr += tmpStr;
if (funds.getNetValueAsset().trim().length() <10) {
tmpStr = funds.getNetValueAsset().trim();
for (int i = 0; i < (10 - funds.getNetValueAsset()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetValueAsset().trim();
}
printStr += tmpStr;
if (funds.getNetTotalValue().trim().length() <10) {
tmpStr = funds.getNetTotalValue().trim();
for (int i = 0; i < (10 - funds.getNetTotalValue()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetTotalValue().trim();
}
printStr += tmpStr;
if (funds.getNetRate().trim().length() <10) {
tmpStr = funds.getNetRate().trim();
for (int i = 0; i < (10 - funds.getNetRate()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getNetRate().trim();
}
printStr += tmpStr;
if (funds.getFundState().trim().length() <20) {
tmpStr = funds.getFundState().trim();
for (int i = 0; i < (20 - funds.getFundState()
.trim().length()); i++) {
tmpStr += " ";
} }
else {
tmpStr = funds.getFundState().trim();
}
printStr += tmpStr;
printStr+=funds.getDateRelease();
return printStr;
}
但是如果字符串里面是中文的话就不行了,求解!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货