java 字符串 cookie_Java基础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码运算差值)

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值