62,614
社区成员
发帖
与我相关
我的任务
分享
public class TestVAlign {
public static void main(String[] args) {
String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!";
int charPerCol = 4;// 每列的字数
TestVAlign testVAlign = new TestVAlign();
String valignString = testVAlign.valign(text, charPerCol);
System.out.print(valignString);
}
private String valign(String text, int charPerCol) {
if (charPerCol == 0)
return "";
if (text == null || text.length() == 0)
return "";
int colCount = getColCount(text, charPerCol);// 列数
int rowCount = getRowCount(text, charPerCol);// 行数
String temp = "";
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
if ((colCount - 1 - col) * charPerCol + row >= text.length())
temp += " ";// 第一列可能不满,需要用空格补齐,这个宽度可能需要自己调整下
else
temp += text.substring((colCount - 1 - col) * charPerCol
+ row, (colCount - 1 - col) * charPerCol + row + 1);
}
temp += "\n";
}
return temp;
}
private int getRowCount(String text, int charPerCol) {
if (text.length() < charPerCol)
return text.length();
else
return charPerCol;
}
private int getColCount(String text, int charPerCol) {
int textLength = text.length();
if (textLength % charPerCol == 0)
return textLength / charPerCol;
else
return textLength / charPerCol + 1;
}
}
public static void main(String[] args) {
System.out.println(valign("楼主莫非要的是这种效果?", 4));
}
public static String valign(String str, int size)
{
int temp = (str.length() - 1) / size + 1;
char[] chars = new char[++temp * size];
char[] source = str.toCharArray();
for (int i = 0; i < chars.length; i++)
{
chars[i] = temp - i % temp < 2 ? '\n' :
(temp - i % temp - 2) * size + i / temp >= source.length ? '~' :
source[(temp - i % temp - 2) * size + i / temp];
}
return new String(chars);
}
public class TestVAlign {
public static void main(String[] args) {
String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!";
int charPerCol = 4;// 每列的字数
TestVAlign testVAlign=new TestVAlign();
String valignString = testVAlign.valign(text, charPerCol);
System.out.print(valignString);
}
private String valign(String text, int charPerCol) {
if (charPerCol == 0)
return "";
if (text == null || text.length() == 0)
return "";
int colCount = getColCount(text, charPerCol);// 列数
int rowCount = getRowCount(text, charPerCol);// 行数
String temp = "";
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
if (col * charPerCol + row >= text.length())
break;//最后一列可能不满
temp += text.substring((col * charPerCol + row), (col
* charPerCol + row) + 1);
}
temp+= "\n";
}
return temp;
}
private int getRowCount(String text, int charPerCol) {
if (text.length() < charPerCol)
return text.length();
else
return charPerCol;
}
private int getColCount(String text, int charPerCol) {
int textLength = text.length();
if (textLength % charPerCol == 0)
return textLength / charPerCol;
else
return textLength / charPerCol + 1;
}
}
public class TestVAlign {
public static void main(String[] args) {
String text = "楼主,你要的就是这种效果吧,希望对你有所帮助!";
int charPerCol = 4;// 每列的字数
new TestVAlign().valign(text, charPerCol);
}
private void valign(String text, int charPerCol) {
if (charPerCol == 0)
return;
if (text == null || text.length() == 0)
return;
int colCount = getColCount(text, charPerCol);// 列数
int rowCount = getRowCount(text, charPerCol);// 行数
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
if (col * charPerCol + row >= text.length())
break;
System.out.print(text.charAt(col * charPerCol + row));
}
System.out.println();
}
}
private int getRowCount(String text, int charPerCol) {
if (text.length() < charPerCol)
return text.length();
else
return charPerCol;
}
private int getColCount(String text, int charPerCol) {
int textLength = text.length();
if (textLength % charPerCol == 0)
return textLength / charPerCol;
else
return textLength / charPerCol + 1;
}
}