zoukankan      html  css  js  c++  java
  • String的一些方法试探

    package countio;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class MyUtil {
        /*
         * 工具类中的方法都是静态方式访问的 因此将构造器私有不允许创建对象(绝对好习惯)
         */
        private MyUtil(){
            throw new AssertionError();
        }
        /*
         * @param filename 文件名
         * @param word  字符串
         * @return 字符串在文件中出现的次数
         */
        public static int countWorInFile(String filename,String word){
            int counter=0;
            FileReader fr = null;
            BufferedReader br = null;
            try{
                fr = new FileReader(filename);
                br = new BufferedReader(fr);//字符缓存输入流(从文件--输入-->程序)
                String line = null;
                while((line = br.readLine())!= null){
                    int index = -1;
                    //每一次读取到的字符串一定大于等于所要找的串hello  indexOf返回某个指定的字符串值在字符串中首次出现的位置
                    while((line.length() >= word.length() && (index =line.indexOf(word))>=0)){
                        counter++;
                        System.out.println(index);//输出4   4 
                        line = line.substring(index+word.length());//从0开始
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(br != null){
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                //fr.close();同上进行先判断后关闭  防止 还没建立就关闭掉资源了        
            }
            return counter;
            
        }
        public static void main(String[] args){
            /*
             * File.separator表示系统相关的分隔符,Linux下为:/ Windows下为:\
             * heiwhellodohehellod(有2个hello)
             */
            //String filename = "\d:\hello.txt\";
            String filename = File.separator+"d:"+File.separator+"hello.txt"+File.separator;
            int counter = countWorInFile(filename,"hello");
            System.out.println("字符串hello出现的次数是:"+counter);
        }
    }

    java中String的一些方法深入解析

    1、public String(char[] c,begin,length).
    从字符数组c的下标begin处开始,将长度为length的字符数组转换为字符串。
    begin与length可以省略,即将字符数组c转换为字符串。另:字符数组可改为字节数组byte[] b.
    char[] c=new char[]{'j','y','6','a','4','t','9'}; 
    String s1=new String(c); 
    String s=new String(c,2,3); 
    System.out.println(s1);
    System.out.println(s);

    
    

    2、public char[] toCharArray().
    字符串装换成字符数组。


    3、public char charAt(int 下标).
    返回字符串中指定位置的字符。
    String s="jkdfsdf";
    char t=s.charAt(3);

    4、public byte[] getBytes().
    将一个字符串转换成字节数组,其默认输出为ASCII值,可通过char强制类型转换输出字节。String s="sjdfsdf";
    byte[] b=s.getBytes();

    5、public String trim().
    清除字符串左右两端的空格。
    String s="skkgnsdfsd   ";
    System.out.println(s.trim());

    6、public int indexOf(String s,int index).
    从字符串中查找指定位置之后指定的字符所在的位置。若不指定位置,则从头开始。
    String s="dgdgdg";
    int n=s.indexOf("t");//从头开始查找
    int n1=s.indexOf("d",3);//从位置3处开始查找

    7、public String substring(int beginindex,int endindex ).
    截取所指定的从开始位置到结束位置的字符串,不包含结束字符。结束位置可以省略。
    String s="sdgsgghd";
    String s1=s.substring(2,4);
    String s2=s.substring(2);

    
    

    8、public String[] split(String s).
    通过指定的字符分割字符串。
    String s="dfgdhdfgdrhrhgdt";
    String ss[]=s.split("d");
    for(int i=0;i<ss.length;i++)
    System.out.println(ss[i]);

    
    

    9、public String toUpperCase()./public String toLowerCase().字符大小写转换。
    String s="dfgdhdfgdrhrhgdt";
    String s1=s.toUpperCase();//字符全大写
    String s2=s.toLowerCase();//字符全小写

    
    

    10、public boolean startsWith(String s)./public boolean endsWith(String s).检测字符串是否是以指定的字符开始/结尾。
    String s="dfdhffghrtgfjn mjg";
    boolean t1=s.startsWith("e");
    boolean t2=s.endsWith("h");

    
    

    11、判断字符串是否相等,区分大小写:equals()。不区分大小写equalsIgnoreCase().
    String s="dfgdghdf";
    String s1="sfsgsdu";
    s.equals(s1);

    
    

    12、public String replaceAll(String s,String s1).将字符串中的s都替换成s1.
    String s="dfgdghdf";
    String s1=s.replaceAll("d","f");

    package stringtest;
    
    
    public class TestString {
    
        public static void main(String[] args) {
            char[] c=new char[]{'j','y','6','a','4','t','9'}; 
            String s1=new String(c); 
            String s=new String(c,2,3); 
        /*    System.out.println(s1.charAt(3));//a
            System.out.println(s);//6a4
    */    
            /*char[] c1 = s1.toCharArray();
            for(int i=0; i<c1.length; i++){
                System.out.print(c1[i]+",");//j,y,6,a,4,t,9,
            }*/
            /*String result="";
            byte[] bytes = s1.getBytes();*/
            
            /*String ss="dgdgdg";
            int n=ss.indexOf("t");//从头开始查找
            int n3=ss.indexOf("d",3);//从位置3处开始查找
            int n1=ss.indexOf("d",1);//从位置1处开始查找
            System.out.println(n);//-1
            System.out.println(n1);//2
            System.out.println(n3);//4
    */        
            String dd="sdgsgghd";
            System.out.println(dd.substring(2,4));//gs
            System.out.println(dd.substring(2));//gsgghd
        }
    }
  • 相关阅读:
    如何使Linux系统上的程序开机后自动运行 (转)
    Makefile详解 (转--不错就是有点长)
    ./configure && make && make install详解 (转)
    从程序员角度看ELF | Linux-Programming (转)
    动态符号链接的细节 (转)
    GCC编译动态和静态链接库例子
    gcc编译静态库和动态库
    udhcp源码详解(五) 之DHCP包--options字段
    一个炒鸡好用的pdf阅读器
    Linux 创建用户 用户组 用户权限
  • 原文地址:https://www.cnblogs.com/hoobey/p/7545366.html
Copyright © 2011-2022 走看看