简体   繁体   English

以相反的方式将每个字符串存储在句子中

[英]store each string in sentence in reverse way

i try with buffered string.reverse(); 我尝试使用缓冲的string.reverse(); but i want reverse each word of sentence. 但是我想把句子的每个词都颠倒过来。 so i try with this.. 所以我尝试一下..

 for(int a = 0;a <= msgLength; a++){
           temp += msg.charAt(a);
           if((msg.charAt(a) == '')||(a == msgLength)){
           for(int b = temp.length()-1; b >= 0; b--){
               encrypt_msg += temp.charAt(a);
               if((b == 0) && (a != msgLength))
                   encrypt_msg += "";
           }
           temp = "";
       }
       }

plz help me to simplify this logic. 请帮助我简化这个逻辑。 string is user defined. 字符串是用户定义的。 i wanted to print reversed string in jtextfields. 我想在jtextfields中打印反向字符串。

Try following simple code: 尝试以下简单代码:

String sentence= "This is sentence";
String[] words=sentence.split(" ");
for(String word: words){
 System.out.println(new StringBuilder(word).reverse());
}

there is a method named split() 有一个名为split()的方法

you can separate the tokens using this method like this. 您可以使用这种方法来分离令牌。

String s = "hello how are you";
String[] s2 = s.split(" ");

now s2 contains each word as array element. 现在s2包含每个单词作为数组元素。

now you can reverese by traversing the array. 现在,您可以遍历数组来查看。

there your answer... 那里你的答案...

StringTokenizer st = new StringTokenizer("this is a test"); StringTokenizer st =新的StringTokenizer(“这是一个测试”); String strRev=""; 字符串strRev =“”;

    while (st.hasMoreTokens()) {
     StringBuffer revers=new StringBuffer(st.nextToken());
     strRev=strRev+" "+revers.reverse().toString();
 }

try it..!! 试试吧..!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM