zoukankan      html  css  js  c++  java
  • Java基础String的方法

    Java基础String的方法

    字符串类型写法格式如下:
    格式一:
    String 变量名称;
    变量名称=赋值(自定义或传入的变量值);
    格式二:
    String 变量名称=赋值(自定义或传入的变量值);
    在输出时任何数据类型与字符串进行拼接,结果一般是字符串
    
    
    public class StringFunc {

    public static void main(String[] args){
    //字符串拼接
    String str1;
    str1 = "hello";
    String str2 = " world";
    System.out.println(str1+str2);
    //字符串与整数拼接
    int num = 100;
    String socers = "得分:";
    System.out.println(socers + num);
    //字符串与对象拼接
    StringFunc Test = new StringFunc();
    System.out.println(socers + Test);
    //字符串方法总结
    String str3 = "abcdef";
    System.out.println("length: "+str3.length()); //查看字符串长度
    System.out.println("concat: "+"xxx".concat("a")); //在结尾默认追加字符串
    System.out.println("replace: "+"aaa".replace("a","z")); //替换字符串种的字符
    System.out.println("isEmpty: "+"".isEmpty()); //判断字符串是否为空
    System.out.println("substring: "+"abcdef".substring(3)); //从首位移除多少个字符
    System.out.println("substring: "+"abcdef".substring(2,5)); //从字符哪截取到哪
    System.out.println("toUpperCase: "+str3.toUpperCase()); //转换小写字母为部大写
    System.out.println("toLowerCase: "+"ABCDEF".toLowerCase()); //转换大写字母为小写
    System.out.println("startsWith: "+"abcd".startsWith("abc")); //判断以什么开头
    System.out.println("endsWith: "+"edef".endsWith("def")); //判断以什么结尾
    String[] list = "a,b,c,d".split(","); //字符串根据分隔符转换成列表的操作
    System.out.println("split: "+list[0]+" "+list[1]+" "+list[2]+" "+list[3]); //打印上面的列表值
    //以下仅作了解
    System.out.println("indexOf: "+str3.indexOf(97)); //输入对应ASCII码整数对应字符下标会返回
    System.out.println("indexOf: "+"abc".indexOf("b")); //判断字符的下标
    System.out.println("hashCode: "+"123".hashCode()); //为这个字符串生成哈希值
    System.out.println("charAt: "+str3.charAt(3)); //返回字符串下表对应的单个字符
    System.out.println("codePointAt: "+str3.codePointAt(1)); //返回字符串对应位置的ASCII码
    System.out.println("codePointBefore: "+str3.codePointBefore(1)); //查看字符串对应位置前一位的ASCII码
    System.out.println("codePointCount: "+str3.codePointCount(1,6)); //查看字符串指定下标长度
    System.out.println("compareTo: "+"z".compareTo("a")); //对比两个字符串相差多少位(利用ASCII码运算差值)
    }
    }

    具体输出如下:

     
  • 相关阅读:
    业务、技术和语言的关系
    查询优化一般原则
    软件构架师的特点(转与Rational Edge)
    VMware安装Vista虚拟机
    使能更改SubVersion日志信息
    清除数据库日志文件
    构建自定义活动以扩展您的工作流的作用范围(转载于MSDN)
    Delphi数据库通用模块及典型系统开发
    Windows Workflow Foundation 2 规则引擎简介
    设置STS服务器配置数据库
  • 原文地址:https://www.cnblogs.com/cookie1026/p/9678020.html
Copyright © 2011-2022 走看看