【问题标题】:How do you take part of a string and move the first few letters to the end of the string您如何获取字符串的一部分并将前几个字母移动到字符串的末尾
【发布时间】:2016-03-07 00:59:53
【问题描述】:

我正在尝试获取用户输入的字符串并将第一个元音之前的第一个字母移动到字符串的末尾。我被困在这里,只需要一个建议。 顺便说一句,这是在java中

【问题讨论】:

标签: java string


【解决方案1】:

找到第一个元音所在的索引(假设 x),然后使用子字符串..

String modified = mystring.substring(x, mystring.length()) + mystring.substring(0, x);

阅读 api 以了解 substring 的实际作用。

【讨论】:

    【解决方案2】:

    好的。这是执行您需要的程序的主要方法。

    public static void main(String args[]){
        String s;
        int i=0;
        //presume s gets user's input here
        for(i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(c=='a'||c=='A'||c=='e' || ...)
            break;
        }
        if(i!=(s.length()-1)){
            for(int j=i;j<s.length();j++)
                System.out.print(s.charAt(j));
            //displays from first vowel to end of string
        }
        else{
            System.out.println("String ain't got any vowels.");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-25
      • 2017-04-12
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 2018-02-25
      相关资源
      最近更新 更多