zoukankan      html  css  js  c++  java
  • 初识java之String与StringBuffer(上)

    好久没写博客了,一直在纠结后面的路怎么发展?好了不说废话了!!正题开始!!

    String与StringBuffer类是我们在开发中最常用的,我们现在一起来分析一下这两个类,首先我们先来谈谈String的方法:

    String s = “123456”;

    1、length()方法:

    这个方法是获取字符串长度的方法,常常用于登陆注册页中判断用户输入的字符长度是否合法;

    例:

    String pswd = "abc1234567";

    if(pawd.length()>6){

    System.out.println("登陆成功!");

    }else{

    System.out.printin("登录失败");

    }

    2、equals()方法

    java中附带了两种字符串比较方法,第一种是equals,这个是严格型的比较,他区分大小;第二种是equalsIsnoreCase(),它是用来判断字符串不区分大小写的比较

    他们都是比较的字符串的值,而"=="是判断字符串在内存中的地址,这两者之间是有区别的;

    例:

    equals方法例子:

    String b = "abc123";
     String c = "ABC123";

     if (b.equals(c)) {
         System.out.println("相等");
      } else {
         System.out.println("不相等");
      }

    equalsIsnoreCase方法例子:

    String b = "abc123";
      String c = "ABC123";
      if (b.equalsIgnoreCase(c)) {
       System.out.println("相等");
      } else {
       System.out.println("不相等");
      }

    "=="使用例子:

    String b = "abc123";
      String c = "abc123";
      if (b==c) {
       System.out.println("相等");
      } else {
       System.out.println("不相等");
      }

    3、toLowerCase()和toUpperCase()的方法使用

    toLowerCase()是把字符串转化为小写形式

    toUpperCase()是把字符串转化为大写形式

    例子:

    String b = "abc123";
      String c = "abc123";
      b.toLowerCase();
      c.toUpperCase();
      System.out.println(b);
      System.out.println(c);

    4、split()方法

    作用是把一个字符串拆分为多个字符串

    例:

    String c = "abc-123";
      String [] d = c.split("-");
      System.out.println(d[0]);
      System.out.println(d[1]);

    5、indexOf()方法

    它的作用是寻找你所查找的字符是在字符串中的那个位置,从零个下标开始

     String b = "abc123";
      String c = "abc-123";
      int d = c.indexOf("2");
      System.out.println(d);

    今天就写在这里咯,下面提几个有意思的问题,大家可以再评论中回答:

    1、equals和"=="有什么区别?

    2、有一个座机号码:0731-76333665,我需要知道它的区号和座机号(区号四位,座机号7位)是否符合规范该如何操作?

    下面推荐一个字符串的博客:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html

  • 相关阅读:
    Python基础之zip和enumerate
    python3中map()函数用法
    python列表推导式
    python面试常问的几个内置装饰器:@staticmethod、@classmethod和@property
    linux的解压与压缩
    python中 s f各种转移字符含义
    fixture 调用函数名传参(转载)
    3.css选择器
    实战有感3
    实战有感2-轮播图
  • 原文地址:https://www.cnblogs.com/sunzan/p/4959227.html
Copyright © 2011-2022 走看看