zoukankan      html  css  js  c++  java
  • java===java基础学习(4)---字符串操作

    java中的字符串操作和python中的大致相同,需要熟悉的就是具体操作形式。

    关于具体api的使用,详见:java===字符串常用API介绍(转)

    package testbotoo;
    
    public class shuzhileixingzhuanhuan {
        
        public static void main(String[] args){
            
            String greeting = "hello word";
            
            //string 类的substring 方法可以实现字符串的提取,提取一个子串。
            String s = greeting.substring(3,8);
            System.out.println(s);  //结果为  lo wo
            
            //字符串的拼接
            int a = 13;
            String c = "hahhah我的";
            String d =  a +c;
            System.out.println(d);
            //不可变字符串
            
            //比如,将greeting的更改为 help!
            greeting = greeting.substring(0,3)+"p!";
            System.out.println(greeting);   //help!
            
    
            
            
            int n = greeting.length();
            System.out.println(n);
            int cpCount = greeting.codePointCount(0,n);
            System.out.println(cpCount);
            
            StringBuilder builder = new StringBuilder();
            builder.append("sdasada");
            builder.append(123);
            
            System.out.println(int length());
            String completedString = builder.toString();  //调用toString方法,可以得到一个string对象
            System.out.println(completedString);
        }
    
    }

            检查字符串是否相等
            表达式:s.equals(t)
            如果相等,返回True.如果不想等,返回False
            **尤其注意的是,不能使用==运算符检测两个字符串是否相等!
            
            空串与Null串
            *空串""是长度为0的字符串
            *Null串,目前没有任何对象与该变量关联。
            可以使用以下代码检查一个字符串是否为空
            if (str.length() == 0)
            或者
            if (str.equals(""))
            可以使用以下代码检查是否为null
            if (str == null)

    转载于:https://www.cnblogs.com/botoo/p/8479029.html

  • 相关阅读:
    BFS(广搜训练题目)
    练习赛1(补题)
    练习赛1(AC题)
    codeup 1743: 算法3-4:表达式求值
    数学相关(更新ing)
    c语言常用函数(更新ing)
    大牛的博客(学习不止,更新不止)
    51nod 1005 大数加法
    js1-----预览js内容
    css10---转载---定位,浮动
  • 原文地址:https://www.cnblogs.com/twodog/p/12137450.html
Copyright © 2011-2022 走看看