大数据兼云计算(王明龙)讲师-JAVA-DAY11-字符串

**

字符串

**

讲解与实例

1.substring(from,end)(含头不含尾)
---------------------------------------------------------------------------------------------------
截取一串字符串的一串子字符串,从from位置的字母(包括from)到end(不包括end位置)的字符串。
可以通过一个字符串+********+一个子字符串的方式隐藏整个字符串中需要隐藏的部分。
用的还是比较多的

2.Integer.valueOf();
---------------------------------------------------------------------------------------------------
String str="888";
int a=Integer.valueOf(str);
System.out.println(a);
字符串不能强转为int型,需要通过Integer.valueOf();转换为int3.trim();
---------------------------------------------------------------------------------------------------
3.1.去除字符串开头和结尾的空字符(空格,tab等)
3.2.Java中字符串创建不可改变,所以trim()后的字符串是新字符串
提示:Scanner 中的sc.next();和sc.nextLine();的区别,next输入的时候一遇到空格就结束,但是nextLine(),遇到空格不结束
  trim()操作后会返回新字符串,创建新字符串对象,未修改就返回原字符串对象。
         trim ()仅去除前后空字符,不会去除中间空字符。

4.charAt()
---------------------------------------------------------------------------------------------------
4.1.charAt(index),返回字符串该下标的字符
4.2.字符'0'  对应 48

5.split()  就是将str字符串分割为四个子字符串,一般化会和上面的几个混用
---------------------------------------------------------------------------------------------------
String str = "you can you up";
String[] str1 = str.split(" ");

6.startsWith和endswidth
---------------------------------------------------------------------------------------------------
6.1.booelan startsWith(str):判断字符串是否是以参数str指定的内容开始
6.2.boolean endsWith(str);  常用于判断文件后缀

7.toUpperCase()和toLowerCase()
---------------------------------------------------------------------------------------------------
统一转换为大写或者是小写

8.valueOf()静态方法
---------------------------------------------------------------------------------------------------
将其他类型转换为字符串类型
char[] 这类型的数组,valueOf返回的是数组拼接后的字符串,但是toString()返回的是输出对象的类型和HashCode。

9.toString()和valueOf()的区别
---------------------------------------------------------------------------------------------------
xx对象.toString();必须先创建对象,再调用对象的toString()方法
String.valueOf(XX对象):静态方法,不需要创建任何对象,就可以直接调用
大多数valueOf方法调用的都是toString()方法,建议大家用valueOf方法,因为valueOf在没有对象也可以用,可以避免空指针异常


String类  常用方法
---------------------------------------------------------------------------------------------------
1、字符串与字符数组之间的转换:
---------------------------------------------------------------------------------------------------
字符串转为字符数组:public char[] toCharArray()
字符数组转为字符串:public String(char[] value)
                  PublicString(char[] value,int offset,int count)

例:
public class StringAPIDemo01{
       public static void main(String args[]){
              String str1 = "hello" ;                  // 定义字符串
              char c[] = str1.toCharArray() ;      // 将一个字符串变为字符数组
              for(int i=0;i<c.length;i++)     // 循环输出
                {System.out.print(c[i] + "、") ; }
              System.out.println("") ;        // 换行
              String str2 = new String(c) ;  // 将全部的字符数组变为String
              String str3 = new String(c,0,3) ;   // 将部分字符数组变为String
              System.out.println(str2) ;             // 输出字符串
              System.out.println(str3) ;             // 输出字符串
       }
};


2、字符串与字节数组之间的转换:
---------------------------------------------------------------------------------------------------
字符串转字节数组:public byte[] getBytes()
字符数组转字符串:public String(byte[] bytes)
                  public String(byte[] bytes,int offset,int length)

例:
public class StringAPIDemo02{
       public static void main(String args[]){
              String str1 = "hello" ;                  // 定义字符串
              byte b[] = str1.getBytes() ;    // 将字符串变为byte数组
              System.out.println(new String(b)) ;      // 将全部的byte数组变为字符串
              System.out.println(new String(b,1,3)) ;       // 将部分的byte数组变为字符串
       }
};

3、字符串与整型数组间的转换:
---------------------------------------------------------------------------------------------------
public class StringAPIDemo03
{public static void main(String[] args)
       {//字符串转为整型数组:
     String s1="123456789";
     int n1[]=new int[s1.length()];
     for(int i=0;i<n1.length;i++)
         n1[i]=Integer. parseInt(String.valueOf(s1.charAt(i)));

    //整型数组转为字符串:
    int n2[]={1,2,3};
    String s2="";
    for(int i=0;i<n2.length;i++)
    s2+=Integer.toString(n2[i]);
    System.out.println(s2);
       }
}

4、获取给定的Index处的字符:
---------------------------------------------------------------------------------------------------
char charAt(int index)

例:
public class StringAPIDemo04{
       public static void main(String args[]){
              String str1 = "java" ;                   // 定义String对象
              System.out.println(str1.charAt(3)) ;     // 取出字符串中第四个字符’a’
       }
};

5、按字典顺序比较两个字符串。
---------------------------------------------------------------------------------------------------
int compareTo(String anotherString)   //区分大小写
int compareToIgnoreCase(String str)   //不区分大小写

例:
public class StringAPIDemo05
{public static void main(String[] args)
       {String s1="abc";
        String s2="aBc";
        //A的Unicode码为65,a的Unicode码为97,所以A<a
        if(s1.compareTo(s2)>0)
           System.out.println("s1>s2");
     if(s1.compareTo(s2)==0)
           System.out.println("s1==s2");
        if(s1.compareTo(s2)<0)
        System.out.println("s1<s2");
       }
}

6、将此字符串与指定的对象比较:
---------------------------------------------------------------------------------------------------
boolean equals(Object anObject)  考虑大小写
boolean equalsIgnoreCase(String anotherString)  不考虑大小写

例:
public class StringAPIDemo06
{public static void main(String[] args)
       {
        String s1 = "abcd";
        String s2 = "Abcd";
     System.out.println("s1是否等于s2:"+s1.equals(s2));            //false
     System.out.println("s1是否等于s2:"+s1.equalsIgnoreCase(s2));   //true
       }
}

7、将指定字符串连接到此字符串的结尾:
---------------------------------------------------------------------------------------------------
String concat(String str)

例:
public class StringAPIDemo07
{public static void main(String[] args)
       {String s1="abc";
        String s2="def";
     System.out.println(s1.concat(s2));
        //输出:abcdef
       }
}

8、copyValueOf返回指定数组中表示该字符序列的String:
---------------------------------------------------------------------------------------------------
String copyValueOf(char[] data)  
data - 字符数组;  返回:一个 String,它包含字符数组的字符。
String copyValueOf(char[] data, int offset, int count)
data - 字符数组。offset - 子数组的初始偏移量。count - 子数组的长度;返回:一个 String,它包含字符数组的指定子数组的字符。

例:
public class StringAPIDemo08
{public static void main(String[] args)
  {
   char[] c=new char[]{'a','b','c','d'};
   System.out.println(String.copyValueOf(c));
   /*返回有c中所有元素构成的字符串,相当于String s=new String(c);
   结果就是产生一个 "abcd "字符串*/

   System.out.println(String.copyValueOf(c,2,2));
   /*返回由c中从下标2的元素(就是 'c ')开始,长度为2的元素构成的字符串,
   结果就是产生一个 "cd "字符串。*/
   }
}

9、startsWith与endsWith:
---------------------------------------------------------------------------------------------------
boolean startsWith(String prefix)   测试此字符串是否以指定的前缀开始
boolean endsWith(String suffix)    测试此字符串是否以指定的后缀结束

例:
public class StringAPIDemo09
{public static void main(String[] args)
       {
        String str1 = "**HELLO" ;                // 定义字符串
        String str2 = "HELLO**" ;                // 定义字符串
        if(str1.startsWith("**"))                    // 判断是否以“**”开头
              {System.out.println(str1+"以**开头") ;}
        if(str2.endsWith("**"))              // 判断是否以“**”结尾
              {System.out.println(str2+"以**结尾") ;}
       }
}

10、大小写字母间的转换:
---------------------------------------------------------------------------------------------------
String toLowerCase()  将 String 中的所有字符都转换为小写
String toUpperCase()  将 String 中的所有字符都转换为大写

例:
public class StringAPIDemo10
{public static void main(String[] args)
       {
        String s1 = "Hello";
     System.out.println(s1.toLowerCase());//hello
     System.out.println(s1.toUpperCase());//HELLO
       }
}

11、indexOf返回指定字符(字符串)索引:
---------------------------------------------------------------------------------------------------
int indexOf(char ch||String str)   返回指定字符(字符串)在此字符串中第一次出现处的索引
int indexOf(char ch||String str, int fromIndex)  返回在此字符串中第一次出现指定字符(字符串)处的索引,从指定的索引开始搜

int lastIndexOf(char ch||String str)  返回指定字符(字符串)在此字符串中最后一次出现处的索引
int lastIndexOf(char ch||String str,int fromIndex) 返回指定字符(字符串)在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索

例:
public class StringAPIDemo11
{public static void main(String[] args)
       {
        String s = "abcbade";
     int n1=s.indexOf('a');       //n1=0
     int n2=s.lastIndexOf('a');    //n2=4
        System.out.println("n1="+n1+",n2="+n2);
     int n3=s.indexOf('b',2);         //n3=3
     int n4=s.lastIndexOf('b',3);      //n3=3
        System.out.println("n3="+n3+",n4="+n4);
     int m1=s.indexOf("bc");        //m1=1
        int m2=s.lastIndexOf("ab");     //m2=4
        System.out.println("m1="+m1+",m2="+m2);
       }
}

12、valueOf:
---------------------------------------------------------------------------------------------------
static String
valueOf(boolean b) 
          返回 boolean 参数的字符串表示形式。
static String
valueOf(char c) 
          返回 char 参数的字符串表示形式。

static String
valueOf(char[] data) 
          返回 char 数组参数的字符串表示形式。

static String
valueOf(char[] data, int offset, int count) 
        返回 char 数组参数的特定子数组的字符串表示形式。

static String

valueOf(double d) 
          返回 double 参数的字符串表示形式。

static String
valueOf(float f) 
          返回 float 参数的字符串表示形式。

static String
valueOf(int i) 
          返回 int 参数的字符串表示形式。

static String
valueOf(long l) 
          返回 long 参数的字符串表示形式。

static String
valueOf(Object obj) 
          返回 Object 参数的字符串表示形式。

例:
public class StringAPIDemo12
{public static void main(String[] args)
       {
        char c[]={'a','b','c','d','e','f'};
        int n=2011;
     String s1=String.valueOf(c);    //字符或字符数组均可转换
        String s2=String.valueOf(c,2,4);
        String s3=String.valueOf(n);   //只有单个整型可转换,整型数组不行
        System.out.println(s1);     //abcdef
        System.out.println(s2);     //cdef
        System.out.println(s3);     //2011
       }
}

13、获取字符串长度length():
---------------------------------------------------------------------------------------------------
int length()

例:
public class StringAPIDemo13
{public static void main(String[] args)
       {
        String s="java";
        System.out.println(s.length());  //返回4
       }
}


14、判断字符串是否为空:
---------------------------------------------------------------------------------------------------
boolean isEmpty()     如果 length() 为 0,则返回 true;否则返回 false

例:
public class StringAPIDemo14
{public static void main(String[] args)
       {
        String s1="";
        String s2="java";
        System.out.println(s1.isEmpty());    //返回true
        System.out.println(s2.isEmpty());    //返回false
       }
}


15、去除字符串前后空格:
String trim()  返回字符串的副本,忽略前导空白和尾部空白

例:
public class StringAPIDemo15
{public static void main(String[] args)
       {
        String s1="   java  word   ";
        String s2="    ";
        String a="a";
        String b="b";
        System.out.println(s1.trim());        //返回“java  word”
        System.out.println(a+s2.trim()+b);   //返回“ab”,s2.trim()为空字符串””
       }
}

16、返回字符串的子串
---------------------------------------------------------------------------------------------------
String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。 参数:beginIndex起始索引(包括)。
String substring(int beginIndex,int endIndex)  返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 参数:beginIndex起始索引(包括);endIndex结束索引(不包括)。

例:
public class StringAPIDemo16
{public static void main(String[] args)
       {
        String s="java word";
        System.out.println(s.substring(1));   //返回“ava word”
        System.out.println(s.substring(1,6)); //返回“ava w”
       }
}

17、替换字符串:
---------------------------------------------------------------------------------------------------
String replace(char oldChar,char newChar)  返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar得到的
String replace(char oldStr,char newStr)  返回一个新的字符串,它是通过用 newStr 替换此字符串中出现的所有 oldStr 得到的

例:
public class StringAPIDemo17
{public static void main(String[] args)
       {
        String s="java";
        System.out.println(s.replace('a','A'));   //返回“jAvA”
        System.out.println(s.replace("ja","JA")); //返回“JAva”
       }
}

18、拆分字符串:
---------------------------------------------------------------------------------------------------
String[ ] split(String regex) 根据给定正则表达式的匹配拆分此字符串

例:
public class StringAPIDemo18
{public static void main(String[] args)
       {
        String s1="hellobbjavabword";
        String[] s2=s1.split("b");
        System.out.println(s2.length);  //返回4
        for(int i=0;i<s2.length;i++)
        System.out.println("s["+i+"]="+s2[i]);
        /*s[0]=hello ,s[1]= ,s[2]=java ,s[3]=word,
        若split里的参数重复出现多次去掉一个,剩下的为空字符串
        如s1中出现bb,所以s2[1]=""  */
        String a="a";
        String b="b";
        System.out.println(a+s2[1]+b);//返回ab
       }

练习

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    /**
     * 过滤空NULL
     * @param o
     * @return 
     */
    public static String FilterNull(Object o) {
        return o != null && !"null".equals(o.toString()) ? o.toString().trim() : "" ;
    }

    /**
     * 是否为空
     * @param o
     * @return
     */
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if ("".equals(FilterNull(o.toString()))) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否不为空
     * @param o
     * @return
     */
    public static boolean isNotEmpty(Object o) {
        if (o == null) {
            return false;
        }
        if ("".equals(FilterNull(o.toString()))) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 是否可转化为数字
     * @param o
     * @return
     */
    public static boolean isNum(Object o) {
        try {
            new BigDecimal(o.toString());
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * 是否可转化为Long型数字
     * @param o
     * @return
     */
    public static boolean isLong(Object o) {
        try {
            new Long(o.toString());
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * 转化为Long型数字, 不可转化时返回0
     * @param o
     * @return
     */
    public static Long toLong(Object o) {
        if (isLong(o)) {
            return new Long(o.toString());
        } else {
            return 0L;
        }
    }

    /**
     * 转化为int型数字, 不可转化时返回0
     * @param o
     * @return
     */
    public static int toInt(Object o) {
        if (isNum(o)) {
            return new Integer(o.toString());
        } else {
            return 0;
        }
    }

    /**
     * 按字符从左截取固定长度字符串, 防止字符串超长, 默认截取50
     * @param o
     * @return
     */
    public static String holdmaxlength(Object o) {
        int maxlength = 50;
        if (o == null) {
            return "";
        }
        return subStringByByte(o, maxlength);
    }

    /**
     * 从左截取固定长度字符串, 防止字符串超长, maxlength为0时默认50
     * @param o
     * @return
     */
    public static String holdmaxlength(Object o, int maxlength) {
        maxlength = maxlength <= 0 ? 50 : maxlength;
        if (o == null) {
            return "";
        }
        return subStringByByte(o, maxlength);
    }

    /**
     * 按字节截取字符串
     * @param str
     * @param len
     * @return
     */
    private static String subStringByByte(Object o, int len) {
        if (o == null) {
            return "";
        }
        String str = o.toString();
        String result = null;
        if (str != null) {
            byte[] a = str.getBytes();
            if (a.length <= len) {
                result = str;
            } else if (len > 0) {
                result = new String(a, 0, len);
                int length = result.length();
                if (str.charAt(length - 1) != result.charAt(length - 1)) {
                    if (length < 2) {
                        result = null;
                    } else {
                        result = result.substring(0, length - 1);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 逗号表达式_添加
     * @param commaexpress 原逗号表达式 如 A,B
     * @param newelement   新增元素 C
     * @return A,B,C
     */
    public static String comma_add(String commaexpress, String newelement) {
        return comma_rect(FilterNull(commaexpress) + "," + FilterNull(newelement));
    }

    /**
     * 逗号表达式_删除
     * @param commaexpress  原逗号表达式 如 A,B,C
     * @param delelement 删除元素 C,A
     * @return B
     */
    public static String comma_del(String commaexpress, String delelement) {
        if ((commaexpress == null) || (delelement == null) || (commaexpress.trim().equals(delelement.trim()))) {
            return "";
        }
        String[] deletelist = delelement.split(",");
        String result = commaexpress;
        for (String delstr : deletelist) {
            result = comma_delone(result, delstr);
        }
        return result;
    }

    /**
     * 逗号表达式_单一删除
     * @param commaexpress  原逗号表达式 如 A,B,C
     * @param delelement 删除元素 C
     * @return A,B
     */
    public static String comma_delone(String commaexpress, String delelement) {
        if ((commaexpress == null) || (delelement == null) || (commaexpress.trim().equals(delelement.trim()))) {
          return "";
        }
        String[] strlist = commaexpress.split(",");
        StringBuffer result = new StringBuffer();
        for (String str : strlist) {
          if ((!str.trim().equals(delelement.trim())) && (!"".equals(str.trim()))) {
            result.append(str.trim() + ",");
          }
        }
        return result.toString().substring(0, result.length() - 1 > 0 ? result.length() - 1 : 0);
      }

    /**
     * 逗号表达式_判断是否包含元素
     * @param commaexpress 逗号表达式 A,B,C
     * @param element C
     * @return true
     */
    public static boolean comma_contains(String commaexpress, String element) {
        boolean flag = false;
        commaexpress = FilterNull(commaexpress);
        element = FilterNull(element);
        if (!"".equals(commaexpress) && !"".equals(element)) {
            String[] strlist = commaexpress.split(",");
            for (String str : strlist) {
                if (str.trim().equals(element.trim())) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    /**
     * 逗号表达式_取交集
     * @param commaexpressA 逗号表达式1  A,B,C
     * @param commaexpressB 逗号表达式2  B,C,D
     * @return B,C
     */
    public static String comma_intersect(String commaexpressA, String commaexpressB) {
        commaexpressA = FilterNull(commaexpressA);
        commaexpressB = FilterNull(commaexpressB);
        StringBuffer result = new StringBuffer();
        String[] strlistA = commaexpressA.split(",");
        String[] strlistB = commaexpressB.split(",");
        for (String boA : strlistA) {
            for (String boB : strlistB) {
                if (boA.trim().equals(boB.trim())) {
                    result.append(boA.trim() + ",");
                }
            }
        }
        return comma_rect(result.toString());
    }

    /**
     * 逗号表达式_规范
     * @param commaexpress  逗号表达式  ,A,B,B,,C
     * @return A,B,C
     */
    public static String comma_rect(String commaexpress) {
        commaexpress = FilterNull(commaexpress);
        String[] strlist = commaexpress.split(",");
        StringBuffer result = new StringBuffer();
        for (String str : strlist) {
            if (!("".equals(str.trim())) && !("," + result.toString() + ",").contains("," + str + ",") && !"null".equals(str)) {
                result.append(str.trim() + ",");
            }
        }
        return result.toString().substring(0, (result.length() - 1 > 0) ? result.length() - 1 : 0);
    }

    /**
     * 逗号表达式_反转
     * @param commaexpress A,B,C
     * @return C,B,A
     */
    public static String comma_reverse(String commaexpress) {
        commaexpress = FilterNull(commaexpress);
        String[] ids = commaexpress.split(",");
        StringBuffer str = new StringBuffer();
        for (int i = ids.length - 1; i >= 0; i--) {
            str.append(ids[i] + ",");
        }
        return comma_rect(str.toString());
    }

    /**
     * 逗号表达式_获取首对象
     * @param commaexpress A,B,C
     * @return A
     */
    public static String comma_first(String commaexpress) {
        commaexpress = FilterNull(commaexpress);
        String[] ids = commaexpress.split(",");
        System.out.println("length:" + ids.length);
        if ((ids != null) && (ids.length > 0)) {
            return ids[0];
        }
        return null;
    }

    /**
     * 逗号表达式_获取尾对象
     * @param commaexpress A,B,C
     * @return C
     */
    public static String comma_last(String commaexpress) {
        commaexpress = FilterNull(commaexpress);
        String[] ids = commaexpress.split(",");
        if ((ids != null) && (ids.length > 0)) {
            return ids[(ids.length - 1)];
        }
        return null;
    }

    /**
     * 替换字符串,支持字符串为空的情形
     * @param strData
     * @param regex
     * @param replacement
     * @return
     */
    public static String replace(String strData, String regex, String replacement) {
        return strData == null ? "" : strData.replaceAll(regex, replacement);
    }

    /**
     * 字符串转为HTML显示字符
     * @param strData
     * @return
     */
    public static String String2HTML(String strData){
        if( strData == null || "".equals(strData) ){
            return "" ;
        }
        strData = replace(strData, "&", "&amp;");
        strData = replace(strData, "<", "&lt;"); 
        strData = replace(strData, ">", "&gt;");
        strData = replace(strData, "\"", "&quot;");
        return strData;
    }

    /**     * 把异常信息转换成字符串,以方便保存 */
    public static String getexceptionInfo(Exception e){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            e.printStackTrace(new PrintStream(baos));
        }finally{
            try {
                baos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return baos.toString();
    }

    /** 过滤特殊符号 */ 
    public static String regex(String str){
        Pattern pattern = Pattern.compile("[0-9-:/ ]");// 中文汉字编码区间
        Matcher matcher;
        char[] array = str.toCharArray();
        for (int i = 0; i < array.length; i++) {
            matcher = pattern.matcher(String.valueOf(array[i]));
            if (!matcher.matches()) {// 空格暂不替换
                str = str.replace(String.valueOf(array[i]), "");// 特殊字符用空字符串替换
            }
        }

        return str;    
    }

    public static String comma_insert(String commaexpress, String newelement,int index){
        int length = commaexpress.length();
        if ( index > length ) {
            index = length;
        }else if ( index < 0){
            index = 0;
        }
        String result = commaexpress.substring(0, index) + newelement + commaexpress.substring(index, commaexpress.length());
        return result;
    }

    /**
     * 将"/"替换成"\"
     * @param strDir
     * @return
     */
    public static String changeDirection(String strDir) {
        String s = "/";
        String a = "\\";
        if (strDir != null && !" ".equals(strDir)) {
            if (strDir.contains(s)) {
                strDir = strDir.replace(s, a);
            }
        }
        return strDir;
    }

    /**
     * 去除字符串中 头和尾的空格,中间的空格保留
     * 
     * @Title: trim
     * @Description: TODO
     * @return String
     * @throws
     */
    public static String trim(String s) {
        int i = s.length();// 字符串最后一个字符的位置
        int j = 0;// 字符串第一个字符
        int k = 0;// 中间变量
        char[] arrayOfChar = s.toCharArray();// 将字符串转换成字符数组
        while ((j < i) && (arrayOfChar[(k + j)] <= ' '))
        ++j;// 确定字符串前面的空格数
        while ((j < i) && (arrayOfChar[(k + i - 1)] <= ' '))
        --i;// 确定字符串后面的空格数
        return (((j > 0) || (i < s.length())) ? s.substring(j, i) : s);// 返回去除空格后的字符串
    }
    /**
     * 得到大括号中的内容
     * @param str
     * @return
     */
    public static String getBrackets(String str) {
        int a = str.indexOf("{");
        int c = str.indexOf("}");
        if (a >= 0 && c >= 0 & c > a) {
            return (str.substring(a + 1, c));
        } else {
            return str;
        }
    }

    /**
     * 将字符串中所有的,替换成|
     * 
     * @param str
     * @return
     */
    public static String commaToVerti(String str) {
        if (str != null && !"".equals(str) && str.contains(",")) {
            return str.replaceAll(",", "|");
        } else {
            return str;
        }
    }

    /**
     * 去掉字符串中、前、后的空格
     * @param args
     * @throws IOException
     */
    public static String extractBlank(String name) {
        if (name != null && !"".equals(name)) {
            return name.replaceAll(" +", "");
        } else {
            return name;
        }
    }

    /**
     * 将null换成""
     * @param str
     * @return
     */
    public static String ConvertStr(String str) {
        return str != null && !"null".equals(str) ? str.trim() : "";
    }

    public static void main(String[] args){
        System.out.println(isNum("a"));
        System.out.println(isNum("-1"));
        System.out.println(isNum("01"));
        System.out.println(isNum("1E3"));
        System.out.println(isNum("1.a"));
        System.out.println(isLong("014650"));
        System.out.println(Long.parseLong("014650"));
    }
}

猜你喜欢

转载自blog.csdn.net/wangminglong1989/article/details/81742851