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 "_";
 }
原文地址:https://www.cnblogs.com/seduce-bug/p/9395191.html