Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void doStuff(String str)
- {
- if (str != null && str != "**here I want to check the 'str' is empty or not**")
- {
- /* handle empty string */
- }
- /* ... */
- }
- if(str != null && !str.isEmpty())
- if(str != null && !str.trim().isEmpty())
- public static boolean empty( final String s ) {
- // Null-safe, short-circuit evaluation.
- return s == null || s.trim().isEmpty();
- }
- if( !empty( str ) )
- import org.apache.commons.lang.StringUtils;
- if (StringUtils.isNotBlank(str)) {
- ...
- }
- if (StringUtils.isBlank(str)) {
- ...
- }
- import android.text.TextUtils;
- if (!TextUtils.isEmpty(str)) {
- ...
- }
- Strings.nullToEmpty(str).isEmpty();
- // or
- Strings.isNullOrEmpty(str);
- Strings.nullToEmpty(str).trim().isEmpty()
- str != null && str.length() != 0
- str != null && !str.equals("")
- str != null && !"".equals(str)
- import com.google.common.base.Strings;
- if (!Strings.isNullOrEmpty(myString)) {
- return myString;
- }
- if(str!= null && str.length() != 0 )
- StringUtils.isNotBlank(str)
- public class ALittleCleaner {
- public List<Employee> findEmployees(String str, int dep) throws ClassNotFoundException, SQLException {
- log("List IN");
- List<Employee> list = Lists.newArrayList();
- Connection con = getConnection();
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- String qry = buildQueryString(str, dep);
- log(qry);
- ResultSet rs = stmt.executeQuery(qry);
- parseResults(list, rs);
- log("List Out");
- return list;
- }
- private void parseResults(List<Employee> list, ResultSet rs) throws SQLException {
- while (rs.next()) {
- Employee employee = new Employee();
- String name = rs.getString(2);
- employee.setName(name);
- int id = rs.getInt(1);
- employee.setId(id);
- int dept = rs.getInt(4);
- employee.setDept(dept);
- int age = rs.getInt(3);
- employee.setAge(age);
- list.add(employee);
- }
- }
- private String buildQueryString(String str, int dep) {
- String qry = "SELECT * FROM PERSON ";
- StringBuilder sb = new StringBuilder();
- if (StringUtils.isNotBlank(str)) {
- sb.append("WHERE NAME LIKE '%").append(str).append("%'");
- log(qry);
- }
- if (dep != 0) {
- if (sb.toString().length() > 0) {
- sb.append(" AND ");
- } else {
- sb.append("WHERE ");
- }
- sb.append("dept=").append(dep);
- }
- qry += sb.append(";").toString();
- return qry;
- }
- private Connection getConnection() throws SQLException, ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/general";
- java.sql.Connection con = DriverManager.getConnection(url, "root", "1234");
- log("URL: " + url);
- log("Connection: " + con);
- return con;
- }
- private void log(String out) {
- // Replace me with a real logger
- System.out.println(out);
- }
- class Employee implements Serializable {
- private static final long serialVersionUID = -8857510821322850260L;
- String name;
- int id, dept, age;
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getId() {
- return this.id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getDept() {
- return this.dept;
- }
- public void setDept(int dept) {
- this.dept = dept;
- }
- public int getAge() {
- return this.age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- }
- /**
- * <p>Checks if a String is whitespace, empty ("") or null.</p>
- *
- * <pre>
- * StringUtils.isBlank(null) = true
- * StringUtils.isBlank("") = true
- * StringUtils.isBlank(" ") = true
- * StringUtils.isBlank("bob") = false
- * StringUtils.isBlank(" bob ") = false
- * </pre>
- *
- * @param str the String to check, may be null
- * @return <code>true</code> if the String is null, empty or whitespace
- * @since 2.0
- */
- public static boolean isBlank(String str) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return true;
- }
- for (int i = 0; i < strLen; i++) {
- if ((Character.isWhitespace(str.charAt(i)) == false)) {
- return false;
- }
- }
- return true;
- }
- if (!"mystring".equals(str)) {
- /* your code here */
- }
- public List<Employee> findEmployees(String str, int dep) {
- Preconditions.checkState(StringUtils.isNotBlank(str), "Invalid input, input is blank or null");
- /** code here **/
- }
- if(!"".equals(str) && str != null) {
- // do stuff.
- }
- public class StringUtils{
- public static boolean areSet(String... strings)
- {
- for(String s : strings)
- if(s == null || s.isEmpty)
- return false;
- return true;
- }
- }
- if(!StringUtils.areSet(firstName,lastName,address)
- {
- //do something
- }
- if(null != str && !str.isEmpty())
- Optional.ofNullable(str)
- .filter(s -> !(s.trim().isEmpty()))
- .ifPresent(result -> {
- // your query setup goes here
- });
- String str1 = "";
- String str2 = null;
- if(StringUtils.isEmpty(str)){
- System.out.println("str1 is null or empty");
- }
- if(StringUtils.isEmpty(str2)){
- System.out.println("str2 is null or empty");
- }
- import com.google.common.base.Strings;
- import org.apache.commons.lang.StringUtils;
- /**
- * Created by hu0983 on 2016.01.13..
- */
- public class StringNotEmptyTesting {
- public static void main(String[] args){
- String a = " ";
- String b = "";
- String c=null;
- System.out.println("Apache:");
- if(!StringUtils.isNotBlank(a)){
- System.out.println(" a is blank");
- }
- if(!StringUtils.isNotBlank(b)){
- System.out.println(" b is blank");
- }
- if(!StringUtils.isNotBlank(c)){
- System.out.println(" c is blank");
- }
- System.out.println("Google:");
- if(Strings.isNullOrEmpty(Strings.emptyToNull(a))){
- System.out.println(" a is NullOrEmpty");
- }
- if(Strings.isNullOrEmpty(b)){
- System.out.println(" b is NullOrEmpty");
- }
- if(Strings.isNullOrEmpty(c)){
- System.out.println(" c is NullOrEmpty");
- }
- }
- }
- private boolean stringNotEmptyOrNull(String st) {
- return st != null && !st.isEmpty();
- }
- org.springframework.util.StringUtils.hasLength(String str)
- org.springframework.util.StringUtils.hasText(String str)
- if(user==null) {
- }else if(user.isEmpty()) {
- }else{
- //code to run
- }
- if(user==null) {
- }else if(user.length() == 0) {
- }else{
- //code to run
- }
- if(user==null) {
- }else if(user.equals("")) {
- }else{
- //code to run
- }
- String str="Nasir";
- if(str.isEmpty()){
- System.out.println("string is empty");
- }
- else{
- System.out.println("string is not empty");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement