zoukankan      html  css  js  c++  java
  • Java学习-课堂总结

    一、字符串比较方式    

         1)‘==’   地址值比较
         2) equals()方法   内容比较

    二、String类的两种实例化方式
         1)String str=“Hello”;
         2)String str=new String(“Hello”);

    三、
    String s=“aa”+“bb”+“cc”;
    String s=“aabbcc”;   //编译器优化

    四、String类的常用方法

    1.public String(char[] value)     描述:将全部的字符数组内容变为字符串

    2.public String(char[] value,int offset,int count)    描述:将部分字符数组变为字符串,offset表示起始点,count表示要操作的长度

    3.public char charAt(int index)    描述:取得指定索引位置上的字符

    课堂编程:

    package com.cqvie.ceshi;

    public class CeShi {

     public static void main(String[] args) {
              char[] ch=new char[] {'a','b','c','d','e','f'};     //定义字符串
              String str1=new String(ch);
              System.out.println(str1);
              String str2=new String(ch,2,3);
              System.out.println(str2);
              char str3=str1.charAt(3);
              System.out.println(str3);  
     }
    }

    运行结果:abcdef

                  cde

                  d

    4.public int length()    描述:取得字符串长度

    课堂编程:

    package com.cqvie.chuan;

    public class Chuan {

     public static void main(String[] args) {
             String s="world";              //定义字符串
             int str=s.length();            //s.length返回值为整形,保存在str
             System.out.println(str);    
     }
    }

    运行结果:5

    5.public int indexOf(String str)    描述:从头查找指定字符串的位置,找不到返回-1

    课堂编程:

    package com.cqvie.chuan;

    public class ChaZhao {

     public static void main(String[] args) {
            String str="今天天气很好";
            int s=str.indexOf("天气");
             System.out.println(s);

     }
    }

    运行结果:2

    6.public int indexOf(String str,int fromIndex)    描述:由指定位置向后查找字符串的位置,找不到返回-1

    课堂编程:

    package com.cqvie.chuan;

    public class ChaZhao1 {

     public static void main(String[] args) {
             String str="昨天今天明天";             //定义字符串
             int s=str.indexOf("天",1);
             System.out.println(s);
             int s1=str.indexOf("天",s+1);
             System.out.println(s1);
     }

    }

    运行结果: 1

                 3

    课堂编程:查询第5个“事'在字符串当中的位置

     package com.cqvie.chuan;

     public class ChaZhao2 {

     public static void main(String[] args) {
          String s="国事家事天下事事事关心";
            int p=-1;
            int n=5;
            String key="事";
      for(int i=0;i<n;i++)
           {
                p=s.indexOf(key, p+1);
           }
               System.out.println(p);
    }
    }

    运行结果:8

                    

  • 相关阅读:
    消息模型:主题和队列的区别
    Navicat12 无限试用(Windows64、Linux、Mac)
    中文技术文档的写作规范
    CentOS7安装GitLab和Jenkins构建CICD环境
    使用Ansible自动化安装MySQL数据库
    CentOS7 Linux生产环境源码编译离线安装Ansible自动化运维工具
    配置Prometheus+Grafana监控主机节点、MySQL、Node、Redis、Mongod、Nginx等
    读书分享——Beyond Feelings: A Guide to Critical Thinking
    Linux云计算架构师及大数据自学资料分享(全集)
    CentOS7安装Rancher企业容器平台
  • 原文地址:https://www.cnblogs.com/tanzk/p/5479640.html
Copyright © 2011-2022 走看看