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码运算差值)
    }
    }

    具体输出如下:

     
  • 相关阅读:
    让IIS支持解析.json格式文件
    请问4-20位字符可由中文,英文,数字及“—”,“-”组成 正则表达式
    jquery中的$(document).ready(function(){})和$(window).load()比较
    根据数组对象的某个属性值找到指定的元素
    JS数组中的indexOf方法
    bat修改注册表
    生动详细解释javascript的冒泡和捕获
    Jquery Easyui与Jquery Bootstrap的比较
    缺少的文件是 ..packagesMicrosoft.Net.Compilers.1.0.0uildMicrosoft.Net.Compilers.props。
    C#中ToString()格式详解
  • 原文地址:https://www.cnblogs.com/cookie1026/p/9678020.html
Copyright © 2011-2022 走看看