Java: 需要使用 .substring(0,42) 方法打印字符串中的空格
Java: Need to print empty spaces in the string using .substring(0,42) method
我是编程的初学者,但我发现这很容易,直到我的导师要我们创建一个静态 space(广告牌)并在其中插入一个用户定义的字符串 space。我想出了如何接受所有用户的输入作为一个字符串。我什至想出了如何将要在定义的 space 内打印的字符串切割成多个部分。但是,如果用户输入太短,我会收到 OutOfBoundsException。如果字符串太长,它会打印到所有可用的、用户定义的 space;然后它再次在定义的 space 下方打印整个字符串。
我需要知道如何接受空的 space。我还需要知道如何限制打印的字符数。
代码如下:
//this is a subclass of the Billboard superclass
package itsd322_u4_ip;
import java.util.Scanner;
public class Create extends Billboard {
Scanner scan = new Scanner(System.in);
String userInput = "";
{
System.out.println("Enter your message [0-336 caharacters]:\n");
userInput = scan.nextLine();
System.out.println("**********************************************");
System.out.println("* *");
System.out.println("* Your message was: *");
System.out.println("* *");
System.out.println("* " + userInput.substring(0,42) + " *");
System.out.println("* " + userInput.substring(42,84) + " *");
System.out.println("* " + userInput.substring(84,126) + " *");
System.out.println("* " + userInput.substring(126,168) + " *");
System.out.println("* " + userInput.substring(168,210) + " *");
System.out.println("* " + userInput.substring(210,252) + " *");
System.out.println("* " + userInput.substring(252,294) + " *");
System.out.println("* " + userInput.substring(294,336) + " *");
System.out.println("* *");
System.out.println("**********************************************");
}
@Override
public void displayInfo()
{
System.out.println(userInput);
System.out.println();
}
}
或许不用
System.out.println("* " + userInput.substring(0,42) + " *");
System.out.println("* " + userInput.substring(42,84) + " *");
System.out.println("* " + userInput.substring(84,126) + " *");
System.out.println("* " + userInput.substring(126,168) + " *");
System.out.println("* " + userInput.substring(168,210) + " *");
System.out.println("* " + userInput.substring(210,252) + " *");
System.out.println("* " + userInput.substring(252,294) + " *");
System.out.println("* " + userInput.substring(294,336) + " *");
您可以使用 for 循环遍历所有可能的子字符串。像这样:
for(int i = 0; i < userinput.length()/42 + 1; i++){
System.out.println("* " + userInput.substring(42*i, 42*i + 42) + " *");
}
这样字符串就可以随心所欲。
for (int i = 0; i < userInput.length(); i += 42) {
System.out.printf("* %-42s *\n", userInput.substring(i, Math.min(i + 42, userInput.length())));
}
@ScottRLemon 的建议确保 substring
的起始索引将始终有效。然而,你也必须对结束索引做同样的事情(这就是 Math.min
调用在这里所做的),为了好看的目的,你必须确保给定的行有 42 个字符,即使是较短的部分被打印(可以 - 通常确实 - 发生在字符串的末尾)。格式字符串 %s
用于字符串,%42s
用于 42 个字符,%-42s
用于 42 个字符,其中较短的字符串向左对齐。
printf
打印,这里没问题。但是,如果您需要存储结果,String.format
可以这样做:
String somestring=String.format("%-42s", "Hello");
一种保持电路板尺寸的方法:
int i=0;
while(i < userInput.length()){
System.out.printf("* %42s *\n", userInput.substring(i, Math.min(i + 42, userInput.length())));
i += 42;
}
while(i < 336){
System.out.println("* *");
i+=42;
}
我是编程的初学者,但我发现这很容易,直到我的导师要我们创建一个静态 space(广告牌)并在其中插入一个用户定义的字符串 space。我想出了如何接受所有用户的输入作为一个字符串。我什至想出了如何将要在定义的 space 内打印的字符串切割成多个部分。但是,如果用户输入太短,我会收到 OutOfBoundsException。如果字符串太长,它会打印到所有可用的、用户定义的 space;然后它再次在定义的 space 下方打印整个字符串。
我需要知道如何接受空的 space。我还需要知道如何限制打印的字符数。
代码如下:
//this is a subclass of the Billboard superclass
package itsd322_u4_ip;
import java.util.Scanner;
public class Create extends Billboard {
Scanner scan = new Scanner(System.in);
String userInput = "";
{
System.out.println("Enter your message [0-336 caharacters]:\n");
userInput = scan.nextLine();
System.out.println("**********************************************");
System.out.println("* *");
System.out.println("* Your message was: *");
System.out.println("* *");
System.out.println("* " + userInput.substring(0,42) + " *");
System.out.println("* " + userInput.substring(42,84) + " *");
System.out.println("* " + userInput.substring(84,126) + " *");
System.out.println("* " + userInput.substring(126,168) + " *");
System.out.println("* " + userInput.substring(168,210) + " *");
System.out.println("* " + userInput.substring(210,252) + " *");
System.out.println("* " + userInput.substring(252,294) + " *");
System.out.println("* " + userInput.substring(294,336) + " *");
System.out.println("* *");
System.out.println("**********************************************");
}
@Override
public void displayInfo()
{
System.out.println(userInput);
System.out.println();
}
}
或许不用
System.out.println("* " + userInput.substring(0,42) + " *");
System.out.println("* " + userInput.substring(42,84) + " *");
System.out.println("* " + userInput.substring(84,126) + " *");
System.out.println("* " + userInput.substring(126,168) + " *");
System.out.println("* " + userInput.substring(168,210) + " *");
System.out.println("* " + userInput.substring(210,252) + " *");
System.out.println("* " + userInput.substring(252,294) + " *");
System.out.println("* " + userInput.substring(294,336) + " *");
您可以使用 for 循环遍历所有可能的子字符串。像这样:
for(int i = 0; i < userinput.length()/42 + 1; i++){
System.out.println("* " + userInput.substring(42*i, 42*i + 42) + " *");
}
这样字符串就可以随心所欲。
for (int i = 0; i < userInput.length(); i += 42) {
System.out.printf("* %-42s *\n", userInput.substring(i, Math.min(i + 42, userInput.length())));
}
@ScottRLemon 的建议确保 substring
的起始索引将始终有效。然而,你也必须对结束索引做同样的事情(这就是 Math.min
调用在这里所做的),为了好看的目的,你必须确保给定的行有 42 个字符,即使是较短的部分被打印(可以 - 通常确实 - 发生在字符串的末尾)。格式字符串 %s
用于字符串,%42s
用于 42 个字符,%-42s
用于 42 个字符,其中较短的字符串向左对齐。
printf
打印,这里没问题。但是,如果您需要存储结果,String.format
可以这样做:
String somestring=String.format("%-42s", "Hello");
一种保持电路板尺寸的方法:
int i=0;
while(i < userInput.length()){
System.out.printf("* %42s *\n", userInput.substring(i, Math.min(i + 42, userInput.length())));
i += 42;
}
while(i < 336){
System.out.println("* *");
i+=42;
}