Java 如何创建一个在循环模式中将破折号插入字符串的方法?
对于上下文,该方法需要在1、2、4、1、2、4。。。图案例如,保存“Overflow”的字符串将输出为“O-ve-rflo-w”。在这种情况下,我会使用嵌套for循环吗?下面是一个简单的硬编码示例。也许您可以找到一种方法,在代码中使用模数Java 如何创建一个在循环模式中将破折号插入字符串的方法?,java,methods,Java,Methods,对于上下文,该方法需要在1、2、4、1、2、4。。。图案例如,保存“Overflow”的字符串将输出为“O-ve-rflo-w”。在这种情况下,我会使用嵌套for循环吗?下面是一个简单的硬编码示例。也许您可以找到一种方法,在代码中使用模数%来表示比“overflow”长的单词 主类{ 公共静态字符串addDash(字符串s) { 字符串s_加_破折号=”; 对于(int i=0;i
%
来表示比“overflow”长的单词
主类{
公共静态字符串addDash(字符串s)
{
字符串s_加_破折号=”;
对于(int i=0;i
以下是一个简单的硬编码示例,用于说明您的情况。也许您可以找到一种方法,在代码中使用模数%
来表示比“overflow”长的单词
主类{
公共静态字符串addDash(字符串s)
{
字符串s_加_破折号=”;
对于(int i=0;i
您可以执行以下操作:
private static String applyPattern(List<Integer> pattern, String str) {
int currentPatternIndex = 0;
int iterationsTillNextDash = pattern.get(currentPatternIndex);
StringBuilder stringBuilder = new StringBuilder();
for (char aChar : str.toCharArray()) {
if (iterationsTillNextDash == 0) {
stringBuilder.append('-');
iterationsTillNextDash = pattern.get(++currentPatternIndex % pattern.size());
}
iterationsTillNextDash--;
stringBuilder.append(aChar);
}
return stringBuilder.toString();
}
输出:
O-ve-rflo-w
O-ve-rflo-w
您可以执行以下操作:
private static String applyPattern(List<Integer> pattern, String str) {
int currentPatternIndex = 0;
int iterationsTillNextDash = pattern.get(currentPatternIndex);
StringBuilder stringBuilder = new StringBuilder();
for (char aChar : str.toCharArray()) {
if (iterationsTillNextDash == 0) {
stringBuilder.append('-');
iterationsTillNextDash = pattern.get(++currentPatternIndex % pattern.size());
}
iterationsTillNextDash--;
stringBuilder.append(aChar);
}
return stringBuilder.toString();
}
输出:
O-ve-rflo-w
O-ve-rflo-w
使用模式的另一个答案是一个很好的解决方案,但是,您也可以使用递归方法。这可能不是一个紧凑的解决方案,但逻辑很容易遵循:
//Process the string in chunks of 7 characters
public static String addFormatting(String input){
String formatted = "";
//Add first character
if(input.length() >= 1) formatted = input.substring(0, 1);
//Add dash and the next 2 characters, else the remainder of the string
if(input.length() >= 3) formatted += "-" + input.substring(1,3);
else if (input.length() > 1) formatted += "-" + input.substring(1);
//Add dash and the next 4 characters, else the remainder of the string
if(input.length() >= 7) formatted += "-" + input.substring(3,7);
else if (input.length() > 3) formatted += "-" + input.substring(3);
//Add dash and recursivly format the next chunk
if(input.length() > 7){
formatted += "-";
return formatted + addFormatting(input.substring(7));
}
//else return the complete formatted once it has been fully processed
else return formatted;
}
要调用该方法,只需使用
addFormatting(“overflowsagreetqnasite!”)代码>打印输出将O-ve-rflo-w-is-agre-a-tQ-nAsi-t-e代码>使用模式的另一个答案是一个很好的解决方案,但是,您也可以使用递归方法。这可能不是一个紧凑的解决方案,但逻辑很容易遵循:
//Process the string in chunks of 7 characters
public static String addFormatting(String input){
String formatted = "";
//Add first character
if(input.length() >= 1) formatted = input.substring(0, 1);
//Add dash and the next 2 characters, else the remainder of the string
if(input.length() >= 3) formatted += "-" + input.substring(1,3);
else if (input.length() > 1) formatted += "-" + input.substring(1);
//Add dash and the next 4 characters, else the remainder of the string
if(input.length() >= 7) formatted += "-" + input.substring(3,7);
else if (input.length() > 3) formatted += "-" + input.substring(3);
//Add dash and recursivly format the next chunk
if(input.length() > 7){
formatted += "-";
return formatted + addFormatting(input.substring(7));
}
//else return the complete formatted once it has been fully processed
else return formatted;
}
要调用该方法,只需使用addFormatting(“overflowsagreetqnasite!”)代码>打印输出将O-ve-rflo-w-is-agre-a-tQ-nAsi-t-e代码>方法:
private static String addDashes(String string, int... pattern) {
String output = "";
int index = 0;
while (true)
for (int p : pattern) {
if (index + p >= string.length())
return output += string.substring(index);
output += string.substring(index, index += p) + "-";
}
}
调用方法:
System.out.println(addDashes("Overflow", 1,2,4));
输出:
O-ve-rflo-w
O-ve-rflo-w
方法:
private static String addDashes(String string, int... pattern) {
String output = "";
int index = 0;
while (true)
for (int p : pattern) {
if (index + p >= string.length())
return output += string.substring(index);
output += string.substring(index, index += p) + "-";
}
}
调用方法:
System.out.println(addDashes("Overflow", 1,2,4));
输出:
O-ve-rflo-w
O-ve-rflo-w
失败,例如,对于文本溢出
,不重复该模式。实际:O-ve-rflo-wOverflowOverflow
,预期:O-ve-rflo-w-Ov-erfl-O-wO-verf-l-ow
@Andreas我字面上说我为他的情况硬编码了它,并推荐了一种可能的解决方案,用于比“Overflow”更长的单词:/是的,您硬编码了1,2,4模式,这是有效的,因为问题并没有将其列为动态的。然而,“Overflow”
文本被明确列为示例,因此硬编码逻辑以仅处理一个示例是没有用的(因此获得否决票)您还跳过了问题的一大部分,即模式重复的部分,再次使答案无效(否决投票的另一个原因)。失败,例如,对于文本溢出
,不重复模式。实际:O-ve-rflo-wOverflowOverflow
,预期:O-ve-rflo-w-Ov-erfl-O-wO-verf-l-ow
@Andreas我字面上说我为他的情况硬编码了它,并推荐了一种可能的解决方案,用于比“Overflow”更长的单词:/是的,您硬编码了1,2,4模式,这是有效的,因为问题并没有将其列为动态的。然而,“Overflow”
文本被明确列为示例,因此硬编码逻辑以仅处理一个示例是没有用的(因此获得否决票)您还跳过了问题的一大部分,即模式重复的部分,再次使答案无效(否决投票的另一个原因)。