Common methods and usage considerations for Java string

Source: Internet
Author: User

Common methods of Java string:

Split () method;

Equals () method;

substring () method;

Example method:

private boolean isSameSelCode(Fbillconfirm fbillconfirm, HashMap outputParam){
    String strExpenseID=new String();

    Fbillconfirmdetail[] fbillconfirmdetail=fbillconfirm.getFbillconfirmdetail();
    if(fbillconfirmdetail!=null&&fbillconfirmdetail.length>0){
        for(int i=0;i<fbillconfirmdetail.length;i++){
            //如果前台传的参数不为Delete状态,即需要新增或修改的数据,需要校验费用
            if(!Constants.DELETED.equals(fbillconfirmdetail[i].getRowstate())){
                strExpenseID+=fbillconfirmdetail[i].getFbcd_expense_id()+";";
            }
        }
    }

    //处理字符串数组传参
    String[] strExpenseIDs=strExpenseID.split(";");

    if(strExpenseIDs.length>0){
        return EpenseSigned.checkSelCode(strExpenseIDs);
    }else{
        return true;
    }

}

Second, Java string considerations:

1.String str= "";--point the handle to a Str object (on the stack, into the pool), string str=new string ();--Create a new object (in the team, not into the pool), the two are different.

Initialization of 2.string[] arrays: string[] The initialization of the STR array needs to define the length, otherwise it cannot be directly assigned, such as str[i]= "123" will be an error. The predefined length required for initialization. Otherwise, you are pointing directly to an existing array.

3.stra.eqauls (STRB)--stra is not nullable, otherwise null, STRA==STRB: Compares two referenced values (that is, the value of the pointer), STRA==STRB: Compares the values of two objects.

4.split Method:

public static void splitString() {
  // 定义一个字符串变量
  String strUser = "Zhangshan,Lisi,Wangma";
  // 切割
  String[] strsUser = strUser.split(",");

  for (int i = 0; i < strsUser.length; i++) {
      System.out.println(strsUser[i]);
  }

 }

Print results:

Zhangshan

Lisi

Wangma

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.