如何在java中反转字符串中的单词而不使用split和stringtokenizer
我想在不使用split方法和如何在java中反转字符串中的单词而不使用split和stringtokenizer,java,string,reverse,Java,String,Reverse,我想在不使用split方法和StringTokenizer的情况下反转java字符串中的单词 例如,你怎么样必须在你怎么样中打印 我试过了,但没有成功 任何帮助都将不胜感激。请尝试下面的代码片段 import java.util.ArrayList; public class ReverseString { public static void main(String args[]) { String myName = "Here we go";
StringTokenizer
的情况下反转java字符串中的单词
例如,你怎么样
必须在你怎么样
中打印
我试过了,但没有成功
任何帮助都将不胜感激。请尝试下面的代码片段
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);
}
}
请尝试下面的代码片段
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);
}
}
这是另一种基于“C”中字符串反转的旧时间逻辑的风格,由此而来
类测试员{
公共静态void main(字符串[]args){
字符串testStr=“LongString”;
测试员u=新测试员();
u、 反向(testStr);
}
公共void reverseStr(字符串testStr){
char[]d=testStr.toCharArray();
int i;
int-length=d.length;
int last_pos;
最后位置=d.长度-1;
对于(i=0;i,这里有另一种风格,基于“C”中字符串反转的旧时间逻辑
类测试员{
公共静态void main(字符串[]args){
字符串testStr=“LongString”;
测试员u=新测试员();
u、 反向(testStr);
}
公共void reverseStr(字符串testStr){
char[]d=testStr.toCharArray();
int i;
int-length=d.length;
int last_pos;
最后位置=d.长度-1;
对于(i=0;i我会这样做:
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();
}
我会这样做:
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();
}
我们都误读了你的问题:)可能重复的我们都误读了你的问题:)可能重复的非常感谢。像charm一样工作。非常感谢。像charm一样工作。或者你也可以使用字符串类的lastIndexOf()和StringBuilder的append()。或者你也可以使用字符串类的lastIndexOf()和append()来自StringBuilder。