如何在不使用split和stringtokenizer的情况下在java中反转字符串中的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19890974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:05:15  来源:igfitidea点击:

How to reverse words in string in java without using split and stringtokenizer

javastringreverse

提问by Raaaj

I want to reverse words in string of java without using split method and StringTokenizer. For ex. How are youmust be printed in you are How. I tried but I failed to do it. Any help will be appreciated.

我想在不使用 split 方法和StringTokenizer. 例如。How are you必须打印在you are How. 我试过了,但我没能做到。任何帮助将不胜感激。

采纳答案by Scientist

Try below code snippet

试试下面的代码片段

import java.util.ArrayList;
public class ReverseString
{
    public static void main(String args[])
    {
        String myName = "Here we go";
        ArrayList al = new ArrayList();
        al = recursiveReverseMethod(myName,al);
        al.trimToSize();
        StringBuilder sb = new StringBuilder();
        for(int i = al.size()-1; i>=0;i--)
        {
            sb.append(al.get(i)+" ");

        }
        System.out.println(sb);

    }
    public static ArrayList  recursiveReverseMethod(String myName,ArrayList al)
    {

        int index = myName.indexOf(" ");
        al.add(myName.substring(0, index));
        myName  = myName.substring(index+1);
        if(myName.indexOf(" ")==-1)
        {
            al.add(myName.substring(0));
            return al;
        }
        return recursiveReverseMethod(myName,al);

    }
}

回答by sathish_at_madison

Here is another flavor based on the old time logic of String reversal in 'C'., from this thread.,

这是基于'C'中字符串反转的旧时间逻辑的另一种风格。,来自这个线程。,

class testers {
    public static void main(String[] args) {
        String testStr="LongString";
        testers u= new testers();
        u.reverseStr(testStr);
    }
    public void reverseStr(String testStr){
        char[] d= testStr.toCharArray();
        int i;
                int length=d.length;
                int last_pos;
                last_pos=d.length-1;
                for (i=0;i<length/2;i++){
                    char tmp=d[i];
                    d[i]=d[last_pos-i];
                    d[last_pos-i]=tmp;

                }
                System.out.println(d);
              }
    }

回答by PratLee

I would do this:

我会这样做:

public static String reverseWordsWithoutSplit(String sentence){
    if (sentence == null || sentence.isEmpty()) return sentence;
    int nextSpaceIndex = 0;
    int wordStartIndex = 0;
    int length = sentence.length();
    StringBuilder reversedSentence = new StringBuilder();
    while (nextSpaceIndex > -1){
        nextSpaceIndex = sentence.indexOf(' ', wordStartIndex);
        if (nextSpaceIndex > -1) reversedSentence.insert(0, sentence.substring(wordStartIndex, nextSpaceIndex)).insert(0, ' ');
        else reversedSentence.insert(0, sentence.subSequence(wordStartIndex, length));
        wordStartIndex = nextSpaceIndex + 1;
    }

    return reversedSentence.toString();
}