gpt4 book ai didi

java - java中的字符串格式化函数

转载 作者:行者123 更新时间:2023-12-01 19:34:08 25 4
gpt4 key购买 nike

我正在编写一个函数来格式化字符串输入。作用是使文本输出左缩进,并限制一行字符数。如果单词之间有多余的空格“”;我跳过多余的空格,只放一个空格' '。如果单词太长并且将超过每行字符限制,我会插入空格直到限制,并在换行符中使用该单词开始新行。

我无法创建代码的填充部分。这是检查单词是否超出限制并以换行符中的单词开始换行的部分。

作为作业要求的一部分,我只允许在字符串上使用 charAt() 和 length 方法

 void runApp(){
String text = " Knowing You Jesus " +
" Graham Kendrick " +
"All I once held dear, built my life upon " +
"All this world reveres, and wars to own " +
"All I once thought gain I have counted loss " +
"Spent and worthless now, compared to this " +
"Knowing you, Jesus " +
"Knowing you, there is no greater thing " +
"You're my all, you're the best " +
"You're my joy, my righteousness " +
"And I love you, Lord " +
"Now my heart's desire is to know you more " +
"To be found in you and known as yours " +
"To possess by faith what I could not earn " +
"All-surpassing gift of righteousness " +
"Oh, to know the power of your risen life " +
"And to know You in Your sufferings " +
"To become like you in your death, my Lord " +
"So with you to live and never die " +
"Source: Musixmatch " +
"Songwriters: Tim Hughes / Ben Cantelon ";

//limit = 60
System.out.println(" `123456789012345678901234567890123456789012345678901234567890");`
System.out.println(leftTextFormat(text, 60));
System.out.println();
}


// This prints out everything left indented, but does not pad.
String leftTextFormat(String text, int limit){
String formattedText = "";
int charCount = 0;
formattedText = formattedText+"[";
for (int i=0; i<text.length(); i++){
if (charCount%limit ==0){
if (text.charAt(i) == ' '){
continue;
}else if (text.charAt(i) != ' ' ){
formattedText = formattedText+text.charAt(i);
charCount++;
}
}else if (charCount%limit != 0){
if (text.charAt(i) == ' '){
if (text.charAt(i-1) != ' '){
formattedText = formattedText+text.charAt(i);
charCount++;
}else{
continue;
}
}else if (text.charAt(i) != ' '){
formattedText = formattedText+text.charAt(i);
charCount++;
}
}
if (charCount%limit ==0 && charCount!=0){
formattedText = formattedText+"]\n[";
}
}
return formattedText;
}

预期输出是这样的: https://drive.google.com/file/d/1uYXtSBo37sFnpwJeBGtjF0MFYNngtZXv/view?usp=sharing

我设法做到的是: https://drive.google.com/file/d/102zNMe4JhaO2IUoOPCS5GSZ02Pq9aXwX/view?usp=sharing

 123456789012345678901234567890123456789012345678901234567890
[Knowing You Jesus Graham Kendrick All I once held dear, buil]
[t my life upon All this world reveres, and wars to own All I]
[once thought gain I have counted loss Spent and worthless no]
[w, compared to this Knowing you, Jesus Knowing you, there is]
[no greater thing You're my all, you're the best You're my jo]
[y, my righteousness And I love you, Lord Now my heart's desi]
[re is to know you more To be found in you and known as yours]
[To possess by faith what I could not earn All-surpassing gif]
[t of righteousness Oh, to know the power of your risen life ]
[And to know You in Your sufferings To become like you in you]
[r death, my Lord So with you to live and never die Source: M]
[usixmatch Songwriters: Tim Hughes / Ben Cantelon

最佳答案

好的尝试,但你的问题是你只添加字符,而从未真正确定这些字符是否组成单词,以及你添加的这些单词是否会超出你的“限制”。因此,您不知道什么时候某个单词实际上会导致您超出您施加的限制,因此当您可能会超出该限制时,您不会尝试填充您的行。

本质上,您需要逐字逐行地工作。每次建立一个单词时,请检查该单词的长度加上当前行的长度是否不超过您的限制。如果它更大,请不要将其连接到当前行,而是将该行填充到限制并开始新行。

public String formattedLine(String singleLine, int limit) {
int padding = limit - singleLine.length();
String pad ="";
for(int j = 0; j < padding; j ++) {
pad += " ";
}
return "[" + singleLine + pad + "]\n";
}

public String leftTextFormat(String text, int limit) {
String word = "";
String singleLine = "";
String formattedText = "";
for(int i=0; i<text.length(); i++) {
if (text.charAt(i) != ' ') {
word = word + text.charAt(i);
} else {
if(word.length() > 0) {
if(singleLine.length() + word.length() >= limit) {
formattedText = formattedText + formattedLine(singleLine, limit);
singleLine = "";
}
if(singleLine.length() == 0) {
singleLine = word;
} else {
singleLine = singleLine + " " + word;
}
word = "";
}
}
}
formattedText = formattedText + formattedLine(singleLine, limit);
return formattedText;
}

关于java - java中的字符串格式化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58461317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com