zoukankan      html  css  js  c++  java
  • java中的字符串一

    public class TestString2 {

    public static void main(String[] args) {
    //判断两字符串是否相等
    String s1 = "Hello world";
    String s2 = "Hello java";
    if (s1.equals(s2)) {
    System.out.println("equal");
    }else{

    System.out.println("not equal");
    }
    System.out.println("------------");
    //判断s1是否以He开头
    if (s1.startsWith("He")) {
    System.out.println("begin with He");
    }
    //判断s1是否以He结尾
    if (s1.endsWith("world")) {
    System.out.println("end with world");
    }
    //返回s1中字母d的下标
    System.out.println("返回s1中字母d的下标为:"+s1.indexOf('d'));
    //返回索引从3到8的子字符串
    System.out.println("返回索引从3到8的子字符串" + s1.substring(3,8));
    //拼接
    System.out.println("拼接到后面"+s1.concat("abcd"));
    //替换单个字母
    System.out.println("替换单个字母"+s1.replace('H','z'));
    //替换第一个字母
    System.out.println("替换第一个字母"+s1.replaceFirst("H","AABB"));
    //是否包含world
    System.out.println("是否包含world "+ s1.contains("world"));
    //根据空格切割
    System.out.println("根据空格切割 "+s1.split(" ")[0] +" " + s1.split(" ")[1] );
    }

    }

  • 相关阅读:
    子数组的最大乘积
    重建二叉树
    只考加法的面试题
    找出发帖的水王问题
    寻找最近点对
    寻找最大的k个数问题
    寻找数组中 的最大值最小值
    数组中的最长递增子序列
    常用的百度API地图操作
    div 背景自适应
  • 原文地址:https://www.cnblogs.com/51testing/p/7872789.html
Copyright © 2011-2022 走看看