A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ↖落葉下♀媃媚 中级黑马   /  2013-4-3 19:26  /  1697 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

string word = "today is beautiful very much";
怎么把单词变成"much very beautiful is today

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

7 个回复

倒序浏览
public static void main(String[] args) throws Exception {
                String word = "today is beautiful very much";
                List<String> li = new ArrayList<String>();
                String [] strs = word.split(" ");
                for(int i =0;i<strs.length;i++){
                        li.add(strs[i]);
                }
                Collections.reverse(li);
               
                for(String s : li){
                        System.out.print(s+" ");
                }
        }
回复 使用道具 举报
string str = "i love you";            
            foreach(string item in str.Split(' ').Reverse())
            {
                Console.Write(item+" ");
            }
            Console.ReadKey();

解法很多,建议同学先用string.split() 得到数组,用数组倒叙输出
回复 使用道具 举报
曾玉锋 发表于 2013-4-3 19:37
string str = "i love you";            
            foreach(string item in str.Split(' ').Reverse())
...

我和你的想法是一样的.....
回复 使用道具 举报
以下代码实现了此功能:
            string word = "today is beautiful very much";

            string[] wordArr = word.Split(' ');           //按空格分割字符串,取出各单词放数组中

            List<string> list = new List<string>();       //list用来保存单词
            for (int i = wordArr.Length - 1; i >= 0; i--) //从后往前遍历数组,将单词放list中,相当于逆序了
            {
                list.Add(wordArr[i]);
            }

            word = string.Join(" ", list);                //将list中的元素用空格连接起来
            Console.WriteLine(word);
            Console.ReadKey();

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
DWC_5101 发表于 2013-4-3 20:56
以下代码实现了此功能:
            string word = "today is beautiful very much";

能告诉我这是在哪课时学的吗? List<string> list = new List<string>();      
回复 使用道具 举报
↖落葉下♀媃媚 发表于 2013-4-4 11:31
能告诉我这是在哪课时学的吗? List list = new List();

自己找资料弄的。
回复 使用道具 举报
我的想法是,先用string里面的split方法,将字符串word分成 5个字符串,存在一个字符串数组中,然后在将字符串数组倒序,最后利用string里面的join函数生成一个新的字符串,就是你要的那个了。代码自己写
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马