zoukankan      html  css  js  c++  java
  • 第06章 字符串的处理

    第6章 字符串的处理

    字符串属于类。
    在Java语言中,处理文本主要应用的类是“String”类和“StringBuffer”类。
    处理小型文本,使用“String”类。
    处理大型文本,使用“StringBuffer”类。

    public class str{
    public static void main(String[] args){
    String str = new String(); //构造函数
    str = "HelloWorld";
    System.out.println(str);
    }
    }

    //说明:
    1、定义字符串对象时,String的首字母是大写的。
    2、构造器就是从类中构造出对象的方法。
    3、字符串一共有9种不同的构造器,下面将详细讲述其中6种。

    1、字符串类的默认构造器
    格式:String()
    String str = new String();

    2、字节参数的构造器
    格式:String(byte[] byte)
    byte[] b = {97, 98, 99};
    String str = new String(b);

    3、获取指定字节数的构造器
    格式:String(byte[] bytes, int offset, int length)
    byte[] b = {97, 98, 99 88, 86, 46};
    String str = new String(b, 3, 2);
    结果:8886

    4、将字节型数据转换为字符集输出的构造器
    格式:String(byte[] bytes, int offset, int length, String charsetName)
    try{
    String str = new String(b, 3, 2, "utf-8");
    System.out.println(str);
    }catch(UnsupportedEncodingException ex){ }

    5、字符数组的构造器
    格式:String(char[] value)
    char[] c = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};
    String str = new String(c);
    System.out.println(str);

    6、截取部分字符串数组内容的构造器:
    格式:String(char[] value, int offset, int count)
    char[] c = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};
    String str = new String(c, 3, 4);

    7、综合应用
    public class rent{
    public static void main(String[] args){
    int a[] = {7, 0, 4, 1, 2, 3, 8, 5, 9, 6, 10};
    int b[] = {5, 2, 0, 6, 6, 8, 9, 1, 5, 8, 8};
    String ls_iphone = new String();
    for(int i=0; i<a.length; i++)
    ls_iphone += b[a[i]];
    System.out.println(ls_iphone);
    }
    }

    第二部分 字符串处理的方法
    1、串连接:+

    2、提取子字符串:substring(int beginIndex, int endIndex)
    substring(int index)
    System.out.println(str.substring(2, 10));
    System.out.println(str.substring(3)); //从第4个字符开始到所有字符。

    String str = "we are students and he is a worker";
    for(int i = 0; i < 34; i++){
    System.out.println(str.substring(i));
    }

    3、从字符串中分解字符
    charAt(int index)
    System.out.println(str.charAt(1));

    4、得到字符串的长度
    System.out.println(str.length());

    5、测试字符串是否相等
    if(str.equals(str1)){

    }

    6、查找特定的子串
    indexOf(子串内容) :如果返回负数,则子串内容不存在。
    startsWith(子串内容) :是否以一个子串开始。
    endsWith(子串内容) :是否以一个子串结尾。

    public class str{
    public static void main(String[] args){
    String ls_str = "你是一个优秀的程序员";
    boolean lb_start, lb_ends;
    int li_pos;
    li_pos = ls_str.indexOf("优");
    lb_start = ls_str.startsWith("程");
    lb_ends = ls_str.endsWith("是");
    System.out.println(ls_str.indexOf("错"));
    System.out.println(li_pos);
    System.out.println(lb_start);
    System.out.println(lb_ends);
    }
    }

    7、从基本类型转换成字符串
    valueOf()

    8、toString()
    String ls_a = "123";
    System.out.println(ls_a.toString());

    9、缓冲字符串处理类 —— StringBuffer
    public class strbuff{
    public static void main(String[] args){
    StringBuffer ls_sba = new StringBuffer(); //实例化字符串对象,默认长度16
    System.out.println(ls_sba.capacity()); //输出字符串的定义长度
    System.out.println(ls_sba.length()); //输出字符串的使用长度

    StringBuffer ls_sbb = new StringBuffer("我是优秀的"); //实例化固定长度的字符串对象默认16 + 5
    System.out.println(ls_sbb.capacity()); //输出字符串的定义长度
    System.out.println(ls_sbb.length()); //输出字符串的使用长度
    ls_sbb.append("程序员"); //追加3个汉字
    System.out.println(ls_sbb.capacity()); //输出字符串的定义长度
    System.out.println(ls_sbb.length()); //输出字符串的使用长度
    System.out.println(ls_sbb);
    }
    }

    10、取字符串的单个字符 —— charAt()
    public class charAt{
    public static void main(String[] args){
    StringBuffer sba = new StringBuffer("小明是一个优秀的程序员");
    System.out.println(sba.capacity()); //输出字符串的定义长度
    System.out.println(sba.length()); //输出字符串的使用长度
    System.out.println(sba.charAt(4));
    }
    }

    11、单个字符串赋值
    public class setCharAt{
    public static void main(String[] args){
    StringBuffer sba = new StringBuffer("小明是一个优秀的程序员");
    System.out.println(sba.capacity()); //输出字符串的定义长度
    System.out.println(sba.length()); //输出字符串的使用长度
    sba.setCharAt(0, '李'); //替换相应位置的字符,
    System.out.println(sba); //该函数不能与println嵌套使用。
    }
    }

    12、指定位置插入字符串
    public class insStrBuff{
    public static void main(String[] args){
    StringBuffer sba = new StringBuffer("我是一个");
    sba.insert(4, "优秀的程序员"); //将字符串插入到指定位置
    System.out.println(sba); //依次插入,不能留空
    }
    }

    13、倒置字符串的内容
    public class strReverse{
    public static void main(String[] args){
    StringBuffer sba = new StringBuffer("我是一个优秀的程序员");
    System.out.println(sba.reverse());
    }
    }

    14、将整数int转换成字符串String的方法有两种:
    String s = String.valueOf(i);
    String s = Integer.toString(i);

  • 相关阅读:
    如何正确得到某个元素的位置
    jQuery Tags Input Plugin(添加/删除标签插件)
    [转载]舒迅:产品经理必读的九步法
    不间断循环滚动代码
    jquery validate api
    jquery文件上传控件 Uploadify
    关于CSS权重问题
    css中fontfamily的中文字体
    expression解决IE6下固定定位的兼容
    模拟下拉select
  • 原文地址:https://www.cnblogs.com/QQ9888267/p/6115232.html
Copyright © 2011-2022 走看看