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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘元霄 黑马帝   /  2012-3-25 18:20  /  2263 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

String st = "1.1.1.1";
String[] str = st.split(".");

我想用 "."来切割 可是  点 为什么不给切, 我用1切能切除3个点,  但用点为什么切不出4个1?  难道是我的电脑问题?

5 个回复

倒序浏览
String[] split(String regex)
          根据给定正则表达式的匹配拆分此字符串。
参数是一个正则表达式的字符串,"."代表的是"*.*",所以不能切割。

评分

参与人数 1技术分 +1 收起 理由
房宝彬 + 1

查看全部评分

回复 使用道具 举报
是不是能转义
回复 使用道具 举报
这样就可以了:
String st = "1.1.1.1";
String[] str = st.split("\\.");

回复 使用道具 举报
import java.util.*;
class MapTest3 {

        public static void main(String[] args) {
                String s=fun("shfwsfvsnwnsnie");
                System.out.println(s);
               
        }
        public static String fun(String s){
                char[] ch=s.toCharArray();
                TreeMap<Character,Integer>  tree= new TreeMap<Character,Integer>();
               
                for(int x=0;x<ch.length;x++)
                                        {
                        if(ch[x]>='a'&&ch[x]<='z' || ch[x]>='A'&&ch[x]<='Z')
                               // continue;
                                                { Integer value=tree.get(ch[x]);
                        if(value==null)
                                                        {
                                tree.put(ch[x], 1);
                                                         }
                                                else
                                                        {
                                value=value+1;
                                tree.put(ch[x], value);
                                                        }
                                                }
                                        }
                        StringBuilder sb=new StringBuilder();
                        Set<Map.Entry<Character, Integer>> entry= tree.entrySet();
                        
                        Iterator<Map.Entry<Character, Integer>> it=entry.iterator();
                        while(it.hasNext()){
                                Map.Entry<Character, Integer> me=it.next();
                                Character key=me.getKey();
                                Integer value1=me.getValue();
                                sb.append(key+"("+value1+")");
                                                                        


                        }
                        
                        
               
                return sb.toString();                                          


}}

哥们,你的代码我给你改完了,测试通过。
主要问题有:1,忘写import java.util.*;了吧。2,如2楼所言,  if(ch[x]>='a'&&ch[x]<='z' || ch[x]>='A'&&ch[x]<='Z'),把continue去掉即可。3,把public class MapTest3 前的public去掉,因为编译时提示: 错误: 类MapTest3是公共的, 应在名为 MapTest3.java 的文件中声明
public class MapTest3 {
        ^
1 个错误
把public去掉后编译通过,至于为什么,我现在不知道。

评分

参与人数 1技术分 +2 收起 理由
职业规划-刘倩老师 + 2 赞一个!

查看全部评分

回复 使用道具 举报
牛秦勇 发表于 2012-3-25 18:59
import java.util.*;
class MapTest3 {

有知道的童鞋,给提示下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马