zoukankan      html  css  js  c++  java
  • 字符串(String)

    String s1 = "abcd";

    String s2 = "efgh";

    将指定字符串连接到此字符串的结尾。

    s1.concat(s2);//结果为:abcdefgh

      public String concat(String str) {

            int otherLen = str.length();

            if (otherLen == 0) {

                return this;

            }

            int len = value.length;

            char buf[] = Arrays.copyOf(value, len + otherLen);

            str.getChars(buf, len);

            return new String(buf, true);

        }

    //s1中是否包含a字符

    System.out.println(s1.contains("a"));//结果为true

    char[] c = {'a','b','c'};

    //new String(c);

    //通过静态方法来创建String类型对象

    String string = String.copyValueOf(c);

    //测试此字符串是否以指定的后缀结束

    String fileName = "abc.txt.doc";

    System.out.println(fileName.endsWith("doc"));

    equals比较

    String s3 = "asd";

    //返回指定字符在此字符串中第一次出现处的索引。

    //如果没有找到则返回-1

    int index = s3.indexOf(97);

    System.out.println("index="+index);//结果为0

    String s3 = "asdfghjkl";

    int indexOf = s3.indexOf("df",2);     

    System.out.println("indexOf="+indexOf);//结果为2

    String s5 = "asdas";

    //返回指定字符在此字符串中  最后一次  出现处的索引。

    System.out.println(s5.lastIndexOf("asd"));结果为0

    返回此字符串的长度。

    s5.length();//结果为5

    // 替换     返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

    String s5 = "asdas";

    String s6 = s5.replace("a","w");//结果为wsdws

    String s6 = s5.replaceAll("a","w");

    System.out.println("s6="+s6);//结果为wsdws

    //使用给定的字符串替换源字符串中第一次出现的字符

    String s5 = "adad";

    String s6 = s5.replaceFirst("asd","你好");

    System.out.println("s6="+s6);

    //分割字符串 (参数为任意字符)

    String s7 = "hello world";

    String[] arr = s7.split(" ");

    for(int i=0;i<arr.length;i++){

    System.out.println(arr[i]);//hello  world

    }

    //第二个参数为结果阈值 (如果阈值过大会按最大分割数组)

    String s7 = "hello world 你好 世界";

    String[] arr = s7.split(" ",2);

    for(int i=0;i<arr.length;i++){

    System.out.println(arr[i]);

       }

       //判断当前字符串是否以指定字符开头

       String s8 = "qer.txt";

       System.out.println(s8.startsWith("qer"));//输出true

       

       //截取从当前索引开始的字符串(包含当前索引位置的值)

       String s9 = "asdfghjkl";

    String s = s9.substring(1);

    System.out.println("s="+s);//输出sdfghjkl

    //源码

     public String substring(int beginIndex) {

            if (beginIndex < 0) {

                throw new  StringIndexOutOfBoundsException(beginIndex);

            }

            int subLen = value.length - beginIndex;

            if (subLen < 0) {

                throw new StringIndexOutOfBoundsException(subLen);

            }

            return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);

        }

    //范围截取(包前不包后)

    String s9 = "asdfghjkl";

    String s = s9.substring(1,3);

    System.out.println("s="+s);

    //源码

      public String substring(int beginIndex, int endIndex) {

            if (beginIndex < 0) {

                throw new StringIndexOutOfBoundsException(beginIndex);

            }

            if (endIndex > value.length) {

                throw new StringIndexOutOfBoundsException(endIndex);

            }

            int subLen = endIndex - beginIndex;

            if (subLen < 0) {

                throw new StringIndexOutOfBoundsException(subLen);

            }

            return ((beginIndex == 0) && (endIndex == value.length)) ? this

                    : new String(value, beginIndex, subLen);

        }

    //把字符串变成字符数组

    //因为每一个字符串都是一个好多字符序列组成的

    //还可以把字符串再拆分成每一个字符

    char[] c = s9.toCharArray();

    for(int i=0;i<c.length;i++)

    System.out.println(c[i]);

    //大小写转换

    String s0 = "ABCDEF";

    //转换为小写

    String s = s0.toLowerCase();

    System.out.println("s="+s);

    //转化为大写

    String s10 = s.toUpperCase();

    System.out.println("s10="+s10);

    //去除头部和尾部的空格

    String s = "    hello world     ";

    System.out.println("s="+s);

    String s11 = s.trim();

    System.out.println("s11="+s11);

       //源码

       public String trim() {

            int len = value.length;

            int st = 0;

            char[] val = value;    /* avoid getfield opcode */

            while ((st < len) && (val[st] <= ' ')) {

                st++;

            }

            while ((st < len) && (val[len - 1] <= ' ')) {

                len--;

            }

            return ((st > 0) || (len < value.length)) ? substring(st, len) : this;

        }

    //把任意类型转换为String类型

    static valueOf(Object obj);

    int i = 9;

    System.out.println(i+1);//10

    String string = String.valueOf(i);

    System.out.println(string+1);//91

    package com.huarui.test;

    Eg:

    String 知识点练习题

    package com.huarui.test;

    public class TestString {

    public static void main(String[] args) {

    String s1 = "abcd";

    String s2 = "abcd";

    //System.out.println(s1.compareTo(s2));

    //System.out.println(s1.compareToIgnoreCase(s2));

    //将指定字符串连接到此字符串的结尾。

    //String s3 = s1+s2;

    //System.out.println(s1.concat(s2));

    //s1中是否包含a字符

    //System.out.println(s1.contains("bc"));

    //s1s2所包含的char值序列是否相等

    //System.out.println(s1.contentEquals(s2));

    /*char[] c = {'a','b','c'};

    //new String(c);

    //通过静态方法来创建String类型对象

    String string = String.copyValueOf(c);*/

    //测试此字符串是否以指定的后缀结束。

    /*String fileName = "abc.txt.doc";

    System.out.println(fileName.endsWith("doc"));*/

    //System.out.println(Character.MIN_SUPPLEMENTARY_CODE_POINT);

    //返回指定字符在此字符串中第一次出现处的索引。

    /*int index = s3.indexOf(102);

    System.out.println("index="+index);*/

    String s3 = "";//null

    String s4 = null;

    /* int indexOf = s3.indexOf("df",2);

    System.out.println("indexOf="+indexOf);*/

    //System.out.println("s3的长度为:"+s3.length());

    //System.out.println(s3.equals(s4));

    //返回指定字符在此字符串中最后一次出现处的索引。

    //System.out.println(s5.lastIndexOf("asd"));

    //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

    /*String s6 = s5.replace("a","w");*/

    //String s6 = s5.replaceAll("a","w");

    /*String s5 = "adad";

    String s6 = s5.replaceFirst("asd","你好");

    System.out.println("s6="+s6);*/

    /*String[] arr = s7.split(" ");

    String s7 = "hello world 你好 世界";

    String[] arr = s7.split(" ",6);

    for(int i=0;i<arr.length;i++){

    System.out.println(arr[i]);

    }*/

    /*String s8 = "qer.txt";

    System.out.println(s8.startsWith("qer"));*/

    //String s = s9.substring(1);

    String s9 = "asdfghjkl";

    /*String s = s9.substring(1,3);

    System.out.println("s="+s);*/

    //因为每一个字符串都是一个好多字符序列组成的

    //还可以把字符串再拆分成每一个字符

    /* char[] c = s9.toCharArray();

    for(int i=0;i<c.length;i++)

    System.out.println(c[i]);*/

    //大小写转换

    /* String s0 = "ABCDEF";

    //转换为小写

    String s = s0.toLowerCase();

    System.out.println("s="+s);

    //转化为大写

    String s10 = s.toUpperCase();

    System.out.println("s10="+s10);*/

    //去除头部和尾部的空格

    /*String s = "    hello world     ";

    System.out.println("s="+s);

    String s11 = s.trim();

    System.out.println("s11="+s11)*/;

    int i = 9;

    System.out.println(i+1);

    String string = String.valueOf(i);

    System.out.println(string+1);

    }

    }

    //1.定义一个方法,接收一个字符串变量,把字符串变量倒序后返回,打印返回后的结果。

    public class TestString {

    public static void main(String[] args) {

    String str = "abcdef";

    //抵用方法得到倒序之后的结果

    String s = showString(str);

    System.out.println("倒序之后的结果为:"+s);

    }

    //自定义的方法  

    private static String showString(String str){

    System.out.println("方法接收到的字符串为:"+str);

    //先把接收到的字符串变成字符数组

    char[] c = str.toCharArray();

    String s = "";

    for(int i=c.length-1;i>=0;i--)

    s += c[i];

    return s;

    }

    }

    package com.huarui.test;

    import org.omg.Messaging.SYNC_WITH_TRANSPORT;

    /*2.定义一个方法,包含两个参数,

    一个是字符串,一个是字符串数组,

    函数的功能是 如果数组中出现参数中的字符串,

    则删除数组中的这个元素,返回删除后的新数组到控制台。*/

    public class TestString2 {

    public static void main(String[] args) {

    String str = "abc";

    String[] arr = {"abcd","abc","ehd","abcd","kkab"};

    show(arr);

    System.out.println("源数组的长度为:"+arr.length);

    String[] arr1 = showString(str,arr);

    show(arr1);

    System.out.println("返回的数组的长度为:"+arr1.length);

    }

    private static void show(String[] arr){

    for(int i=0;i<arr.length;i++)

    System.out.println(arr[i]);

    }

    //删除数组中出现的参数,并返回新数组

    private static String[] showString(String str,String[] arr){

    System.out.println("接收到的数组为:"+arr);

    System.out.println("接收到需要删除的元素为:"+str);

    //查找源数组中含有多少个需要删除的元素

    //定义一个计数器  在创建数组时必须规定长度

    int count = 0;

    for(int i=0;i<arr.length;i++){

    if(str.equals(arr[i])){

    count++;

    }

    }

    //根据需要删除的个数创建新的数组

    String[] arr1 = new String[arr.length-count];

    //把不需要删除的元素添加到新的数组中 arr[1]

    //定义一个计数器

    int j = 0;

    //循环遍历源数组

    for(int i=0;i<arr.length;i++){

    //如果参数与源数组的每个元素值相等时

    if(str.equals(arr[i]))//2

    //结束本次循环

    continue;

    else{

    //System.out.println(arr[i]);

    arr1[j] = arr[i];

    j++;

    }

    }

    return arr1;

    }

    }

    package com.huarui.test;

    /*3.定义一个方法,含有字符串参数,

    将参数中字符串的每个单词首字母大写再显示到文本框中。

    hello world!!!

    拿空格分割字符串

    得到每一个单词

    得到每一个单词首字母

    首字母大写

    Hello World!!!

    */

    public class TestString3 {

    public static void main(String[] args) {

    String s = "hello world!!!";

    String s1 = showString(s);

    System.out.println("大写之后的值为:"+s1);

    }

    private static String showString(String str){

    System.out.println("接收到的参数为:"+str);

    //拿空格分割字符串

    String[] arr = str.split(" ");

    //得到每一个单词

    String string = "";

    for(int i=0;i<arr.length;i++){

    string += arr[i].substring(0,1).toUpperCase()+arr[i].substring(1)+" ";

    /* //每个单词的首字母

    char c = arr[i].charAt(0);

    //char类型没有字幕大写方法  只能转换为String类型

    String s = c+"";

    //s转换成大写

    //得到每一个单词首字母

    //首字母大写

    String s1 = s.toUpperCase();

    string += s1+arr[i].substring(1)+" ";*/

    }

    return string;

    }

    }

    package com.huarui.test;

    /*4.定义一个方法,含有字符串参数,

    分别统计字符串中字母,数字,特殊符号的个数,并打印。

    得到字符串中的每个字符

    判断每个字符是否为数字、字母或者特殊符号

    使用计数器累加

    */

    public class TestString4 {

    public static void main(String[] args) {

    String s = "asd2123!@#$asdd4452";

    showString(s);

    }

    private static void showString(String str){

    System.out.println("接收到的参数为:"+str);

    //把接收到的字符串变成字符数组

    char[] c = str.toCharArray();

    //定义三个计数器

    int a = 0;//表示数字

    int b = 0;//表示字母

    int d = 0;//表示特殊符号

    for(int i=0;i<c.length;i++){

    if(c[i]>='0'&&c[i]<='9'){

    a++;

    }else if(c[i]>='a'&&c[i]<='z'||c[i]>='A'&&c[i]<='Z'){

    b++;

    }else{

    d++;

    }

    }

    System.out.println("数字的个数为:"+a);

    System.out.println("字母的个数为:"+b);

    System.out.println("特殊符号的个数为:"+d);

    }

    }

    SimpleDateFormat

    作用:

    日期-->文本(date-->String

         //创建一个格式化对象

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    //创建一个时间对象

    Date date = new Date();

    System.out.println("date:"+date);

    String time = sdf.format(date);

    System.out.println("time:"+time);

         

    文本-->日期(String-->date)

        String time = "2017-09-19 03:22:18";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    Date date = sdf.parse(time);

    System.out.println(date);

    NUmberFormat

    DecimalFormat df = new DecimalFormat("###.##");

    for (double i = 1; i <= 9; i++) {

    for (double j = 1; j <=i; j++) {

    String num = df.format(j/i);

    System.out.print(j+"/"+i+"="+num+" ");

    }

    System.out.println();

    }

  • 相关阅读:
    Js--Array类型1
    利用js生成一个在线考试系统
    在Asp.net core 项目中操作Mysql数据库
    Unity游戏接入TypeSDK集成笔记
    第一篇博客
    两次面试
    [OC笔记] static 关键字
    cellForRowAtIndexPath方法不执行的那些坑
    一行代码设置UITableView分割线的长度
    [转载]iOS开发:获取设备信息
  • 原文地址:https://www.cnblogs.com/LPJ-BLOG/p/12558793.html
Copyright © 2011-2022 走看看