zoukankan      html  css  js  c++  java
  • day_15

    -暑假学习第十五天--

    一.获取方法

    int length()  获取字符串的长度

    char charAt(int index) 获取特定位置的字符 (角标越界)

    int indexOf(String str) 获取特定字符的位置(overload)

    int lastIndexOf(int ch) 获取最后一个字符的位置

    二.判断方法

    boolean endsWith(String str) 是否以指定字符结束

    boolean isEmpty()是否长度为0 如:“” null V1.6

    boolean contains(CharSequences) 是否包含指定序列 应用:搜索

    boolean equals(Object anObject) 是否相等

    boolean equalsIgnoreCase(String anotherString) 忽略大小写是否相等

    三.转换方法

    String(char[] value) 将字符数组转换为字符串

    String(char[] value, int offset, int count)

    Static String valueOf(char[] data)

    static String valueOf(char[] data, int offset, int count)

    char[] toCharArray()  将字符串转换为字符数组

     

     四.其他方法

    String replace(char oldChar, char newChar) 替换

    String[] split(String regex) 切割

    String substring(int beginIndex)

    String substring(int beginIndex, int endIndex)截取字串

    String toUpperCase() 转大写

    String toLowerCase() 转小写

    String trim() 去除空格

    eg:    去除字符串两边空格的函数。

    public class Demo1 {
    // 定义一个祛除字符串两边空格的函数
    public static String trim( String str ){
       
       // 0、定义求字串需要的起始索引变量
       int start = 0;
       int end = str.length()-1;
       // 1. for循环遍历字符串对象的每一个字符
       for (int i = 0; i<str.length() ; i++ )
       {
            if ( str.charAt(i) == ' ' )
            {
                 start++;
            }else{
            
                 break;
            }
       }
       System.out.println( start );
       for (; end<str.length() && end >= 0;  )
       {
            if ( str.charAt(end) == ' ' )
            {
                 end--;
            }else{
                 break;
            }
       }
       System.out.println( end );
       // 2. 求子串
       if( start < end ){
         
         return str.substring( start , (end+1) ); 
       }else{
         
         return "_";
     }
  • 相关阅读:
    XSS跨站脚本攻击实例讲解,新浪微博XSS漏洞过程分析
    PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
    PHP导出数据到CSV文件函数/方法
    iOS8 Core Image In Swift:视频实时滤镜
    实战:mysql版本号升级
    Apache Shiro 使用手冊 链接文件夹整理
    Windows 驱动开发
    Python标准库:内置函数bytearray([source[, encoding[, errors]]])
    cocos2d-x 2.2.3 之菜单分析(1)
    JSP具体篇——out
  • 原文地址:https://www.cnblogs.com/seduce-bug/p/9395191.html
Copyright © 2011-2022 走看看