gpt4 book ai didi

java - 尝试在字符串中的每个元音之前添加一个字符

转载 作者:行者123 更新时间:2023-12-02 03:56:46 25 4
gpt4 key购买 nike

我想在单词中的每个元音之前插入“ab”

例如,如果用户输入单词:fire它必须更改为:fabirabe但我的代码只在单词之前输入 ab,例如:abfire。我该如何解决这个问题?

这是我到目前为止的代码:

import java.util.Scanner;
public class Foothill
{
// class variables shared by more than one method
String prompt;
static String strUserResponse;

// main method
public static void main (String[] args)
{
giveInstructions();
getUserInput();
convertToTurkeyIrish();
vowelCounter();
}
public static String convertToTurkeyIrish()
{

String turkeyIrish = strUserResponse;
String turkeyIrish2;
turkeyIrish2 = "ab" + strUserResponse.replaceAll("(aeiouAEIOU)", "$1ab");
System.out.println("Word In Turkey Irish: " + turkeyIrish2);
return turkeyIrish;
}

public static void vowelCounter()
{
int vowel = 0;
strUserResponse.length();
char vowels;
vowels = ' ';
for (int j = 0; j <= strUserResponse.length() - 1 ; j++)
{
vowels = strUserResponse.charAt(j);
if ((vowels == 'a') || (vowels == 'A') || (vowels == 'e') || (vowel == 'E') || (vowel == 'i')|| (vowels == 'I') || (vowels == 'o') (vowels == 'O') || (vowel == 'u') || (vowels == 'u'))
{
System.out.println("Vowels in " + strUserResponse + ": " + vowel++);
}
}
}
}

最佳答案

您的正则表达式是错误的,替换字符串也是错误的。试试这个:

strUserResponse.replaceAll("([aeiouAEIOU])", "ab$1");

关于java - 尝试在字符串中的每个元音之前添加一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35370890/

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