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

发布时间:2021-09-04 16:30:42 作者:chen
来源:亿速云 阅读:100

这篇文章主要介绍“java中String的一些常见方法深入解析”,在日常操作中,相信很多人在java中String的一些常见方法深入解析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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    }
}

到此,关于“java中String的一些常见方法深入解析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 深入解析thinkphp中的addAll方法
  2. 深入解析Redis中常见的应用场景

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

string java

上一篇:docker容器镜像命令有哪些

下一篇:MySQL中的隐藏列的具体查看方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》