用freemarker重新定位光标 [英] Repositioning the cursor with freemarker

查看:54
本文介绍了用freemarker重新定位光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用免费的标记和Java.我必须在txt文件上输出结果.假设我必须将3列打印为

I am working with free marker and java. I have to output result on a txt file. Say i have to print for 3 columns as

`A     B      C`

在类似的模式中.所有三个属性都作为字符串. 条件是,如果引用A的字符串的长度大于3,则应在下一行中打印它.也就是说,如果长度为8,则前3个字符将位于第一行,第二至4至6个字符,而其余两个则位于第三行.现在之后,当我必须打印B时,我必须回到第一行,但是A的当前位置将是第3行.

In the similar patter. All three attributes as string. Condition is that, if the length of string referring to A is more than 3 than it should be printed in the next line. That is if the length is 8 then first 3 character will be in first line, 4 to 6 in second and the remaining two in third. Now after that when I have to print for B, i would have to come back to the first line, but the current position for A will be line 3.

我该怎么做?

示例

Hi,                          (required cursor position to print for B).
Hel
lo(current cursor position)

推荐答案

我不知道freemarker,这是仅使用String操作的一种方法:

I don't know freemarker, Here is one approach by just using String manipulation:

  1. 带一个StringBuilder
  2. A,B或C剩下任何字符
  3. 对于A,附加最多提取3个字符
  4. 将空格附加到8个字符
  5. 对于B,追加最多提取3个字符
  6. 将空格附加到8个字符
  7. 对于C,追加最多提取3个字符
  8. 将空格附加到8个字符
  9. 追加换行符
  1. Take a StringBuilder
  2. While A, B or C has any characters left
  3. For A, append extract max 3 characters
  4. append blank spaces till 8 characters
  5. For B, append extract max 3 characters
  6. append blank spaces till 8 characters
  7. For C, append extract max 3 characters
  8. append blank spaces till 8 characters
  9. append newline

这是代码示例,可能没有进行优化,但是可以工作:

Here is code example, might not be optimized but works:

public static void main(String[] args) {
  System.out.println(formatOutput("Hello", "How are you?", "Wassup"));
}

public static String formatOutput(String textA, String textB, String textC) {
    StringBuilder output = new StringBuilder("");

    int beginIndex = 0, endIndex = 3;
    String snippet;
    while (textA.length() > 0 || textB.length() > 0 || textC.length() > 0) {
        if (textA.length() > endIndex) {
            snippet = textA.substring(beginIndex, endIndex);
            textA = textA.substring(endIndex);
        } else {
            snippet = textA.substring(beginIndex);
            textA = "";
        }
        output.append(snippet);
        for (int i = snippet.length(); i <= 8; i++)
            output.append(" ");
        ;

        if (textB.length() > endIndex) {
            snippet = textB.substring(beginIndex, endIndex);
            textB = textB.substring(endIndex);
        } else {
            snippet = textB.substring(beginIndex);
            textB = "";
        }
        output.append(snippet);
        for (int i = snippet.length(); i <= 8; i++)
            output.append(" ");


        if (textC.length() > endIndex) {
            snippet = textC.substring(beginIndex, endIndex);
            textC = textC.substring(endIndex);
        } else {
            snippet = textC.substring(beginIndex);
            textC = "";
        }
        output.append(snippet);
        for (int i = snippet.length(); i <= 3; i++)
            output.append(" ");
        output.append("\n");    
    }
    return output.toString();
}

输出:

Hel      How      Was 
lo        ar      sup 
         e y          
         ou?          

这篇关于用freemarker重新定位光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆