当前位置:编程学习 > JAVA >>

eclipse处理长字符串拼接快捷方法类

情景:
 
你在后台写sql文访问数据库时是不是要这样写
 
 
 
String sql="select a,"  
                 +"b,"  
                 +"c "  
                 +"from table";  

String sql="select a,"
                 +"b,"
                 +"c "
                 +"from table";

 

 
当sql语句短还好,要是长了该写多少个“+”和““”啊,于是就想写一个工具类处理长字符串拼接的问题 
 
 
 
 
代码:
 
 
 
import java.util.ArrayList;  
import java.util.Scanner;  
public class GenSqlString {  
    /** 
     * 处理长字符串拼接 
     * @param args 
     */  
    public static void main(String[] args) {  
        ArrayList<String> array = new ArrayList<String>();  
        Scanner scn = new Scanner(System.in);  
        String line;  
        String reString;  
        System.out.println("请输入您的长字符串");   
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));    
        //String reString="+\""+br.readLine()+" \"";   
        /** 当输入end时回车结束*/  
        while (!"end".equals(line = scn.nextLine())) {  
            array.add(line);  
        }  
        System.out.println("The result is: \r\n");  
        int i=1;  
        for(String str : array){  
            if(i==1){  
                reString="\""+str+" \"";  
            }else if(i==array.size()){  
                reString="+\""+str+" \";";  
            }else{  
                reString="+\""+str+" \"";  
            }  
            System.out.println(reString);  
            i++;  
        }  
    }  
  
}  

import java.util.ArrayList;
import java.util.Scanner;
public class GenSqlString {
/**
* 处理长字符串拼接
* @param args
*/
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
     Scanner scn = new Scanner(System.in);
     String line;
     String reString;
     System.out.println("请输入您的长字符串"); 
     //BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
     //String reString="+\""+br.readLine()+" \"";
     /** 当输入end时回车结束*/
     while (!"end".equals(line = scn.nextLine())) {
array.add(line);
}
     System.out.println("The result is: \r\n");
     int i=1;
     for(String str : array){
     if(i==1){
     reString="\""+str+" \"";
     }else if(i==array.size()){
         reString="+\""+str+" \";";
     }else{
     reString="+\""+str+" \"";
     }
System.out.println(reString);
i++;
}
}

}

 

 
 
 
虽然简单点,但是能用就行,提高开发效率嘛
 
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,