提问人:Nick Slack 提问时间:3/8/2023 最后编辑:AbraNick Slack 更新时间:3/10/2023 访问量:68
如何在 java 中将字符插入到一行的特定位置?
How to insert a character into a specific spot in a line in java?
问:
我环顾四周,没有找到任何我真正理解或可以做的事情,我希望有一个简单的解决方案。我必须在此程序中每行之后的第 20 个字符槽中添加一个。*
这是我的代码:
package StarBorder;
import java.util.Scanner;
public class NewTry {
public static void main(String[] args) {
System.out.println("Please input a word: ");
Scanner stringScanner = new Scanner(System.in);
String input = stringScanner.nextLine();
int numberOfCharacters = 20;
System.out.println("**********************");
for (int i = 0; i < input.length(); i += numberOfCharacters) {
String part = input.substring(i, Math.min(input.length(), i + numberOfCharacters));
System.out.println("*" + part + "*");
}
System.out.println("**********************");
}
}
这是它打印出来的内容:
**********************
*Hello I am doing som*
*e code, hello ther p*
*eople on stack overf*
*low* // I need this to be over where the other *'s are.
**********************
我需要它是一个完整的星星正方形。感谢您的任何回答。
我也希望它不是在第二十个字符处切断,而是在那之前的空间处切断。然而,这是一个次要问题。
答:
0赞
Senuka Liyanage
3/8/2023
#1
一个简单的方法是简单地打印出一堆空格来填充空白(填充字符串)。 例如,对于输入中的最后一行,您可以执行以下操作
System.out.print("*" + part);
for(int i=0; i < numberOfCharacters - 2 - part.length(); i++){
System.out.print(" ");
}
System.out.println("*");
这段代码所做的只是找到零件长度和理想子字符串长度之间的差值,然后打印出那么多空格。
评论
0赞
user3437460
3/8/2023
这是硬编码,在我看来应该避免。
0赞
Senuka Liyanage
3/9/2023
无论如何,他们的页眉和页脚都是硬编码的;但是,我将编辑 for 循环以不具有硬编码的 18
0赞
Joop Eggen
3/8/2023
#2
System.out.printf("*%-20s*%n", part);
这将打印格式化、左对齐 ()、paddel 到 20。 换行符。part
-
%n
0赞
bingfeng
3/9/2023
#3
public static void main(String[] args) {
System.out.println("Please input a word: ");
Scanner stringScanner = new Scanner(System.in);
String input = stringScanner.nextLine();
int numberOfCharacters = 20;
System.out.println("**********************");
for (int i = 0; i < input.length(); i += numberOfCharacters) {
String part = input.substring(i, Math.min(input.length(), i + numberOfCharacters));
System.out.print("*" + part);
// Quoting the comment area answer, there should not be a minus 2 here
for (int j = 0; j < numberOfCharacters - part.length(); j++) {
System.out.print(" ");
}
System.out.println("*");
}
System.out.println("**********************");
}
评论
0赞
bingfeng
3/9/2023
引用评论区的答案,这里不应该有负2
1赞
Abra
3/10/2023
#4
你在问题中写道:
我也希望它不是在第二十个字符处切断,而是在那之前的空间处切断。
请考虑以下代码。它使用方法 printf (of class ) 将每行输出打印为 20 个字符的字符串,带有一个前导星号和单个尾随星号 ()。格式字符串为:java.io.PrintStream
*
*%-20s*%n
这意味着,如果字符串的长度少于 20 个字符,则该字符串左对齐并用尾随空格填充,并且后面跟着换行符。如前所述,字符是文字。*
我建议你用调试器运行它,因为我相信这将有助于你了解它是如何工作的。
import java.util.Scanner;
public class NewTry {
public static void main(String[] args) {
System.out.println("Please input a word: ");
Scanner stringScanner = new Scanner(System.in);
String input = stringScanner.nextLine();
input = input.trim();
int numberOfCharacters = 20;
String format = "*%-" + numberOfCharacters + "s*%n";
System.out.println("**********************");
int total = input.length();
int start = 0;
String hyphen;
String substr;
while (start + numberOfCharacters < total) {
if (input.charAt(start) == ' ') {
start++;
}
hyphen = "";
int index = start + numberOfCharacters;
if (index < total) {
while (index > start && input.charAt(index) != ' ') {
index--;
}
if (index == start) {
index = start + numberOfCharacters - 1;
hyphen = "-";
}
}
substr = input.substring(start, index);
if (substr.charAt(0) == ' ') {
substr = substr.substring(1);
index++;
start++;
int temp = start + numberOfCharacters;
if (temp < total) {
if (input.charAt(temp) == ' ') {
index = temp;
substr = input.substring(start, index);
}
}
}
System.out.printf(format, substr + hyphen);
start = index;
}
if (start < total) {
substr = input.substring(start);
if (substr.charAt(0) == ' ') {
substr = substr.substring(1);
}
System.out.printf(format, substr);
}
System.out.println("**********************");
stringScanner.close();
}
}
上面的代码假定用户输入的字符串如下:
- 字符串不包含标点符号,即逗号、连字符等。
- 字符串中的单词仅由单个空格分隔,即没有 TAB 字符或多个连续空格。
请注意,上面的代码还处理长度超过 20 个字符的单词。它通过获取 19 个字符并添加连字符来实现。
以下是我的测试运行:
Please input a word:
George Best was a professional footballer who began his professional career playing for English football club Manchester United at the age of 17
**********************
*George Best was a *
*professional *
*footballer who began*
*his professional *
*career playing for *
*English football *
*club Manchester *
*United at the age of*
*17 *
**********************
Please input a word:
antidisestablishmentarianism
**********************
*antidisestablishmen-*
*tarianism *
**********************
Please input a word:
can you spell antidisestablishmentarianism
**********************
*can you spell *
*antidisestablishmen-*
*tarianism *
**********************
Please input a word:
antidisestablishmentarianism is a long word
**********************
*antidisestablishmen-*
*tarianism is a long *
*word *
**********************
Please input a word:
please find the word antidisestablishmentarianism in the dictionary
**********************
*please find the word*
*antidisestablishmen-*
*tarianism in the *
*dictionary *
**********************
Please input a word:
stackoverflow
**********************
*stackoverflow *
**********************
评论
*
*