Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. public void doStuff(String str)
  2. {
  3. if (str != null && str != "**here I want to check the 'str' is empty or not**")
  4. {
  5. /* handle empty string */
  6. }
  7. /* ... */
  8. }
  9.  
  10. if(str != null && !str.isEmpty())
  11.  
  12. if(str != null && !str.trim().isEmpty())
  13.  
  14. public static boolean empty( final String s ) {
  15. // Null-safe, short-circuit evaluation.
  16. return s == null || s.trim().isEmpty();
  17. }
  18.  
  19. if( !empty( str ) )
  20.  
  21. import org.apache.commons.lang.StringUtils;
  22.  
  23. if (StringUtils.isNotBlank(str)) {
  24. ...
  25. }
  26.  
  27. if (StringUtils.isBlank(str)) {
  28. ...
  29. }
  30.  
  31. import android.text.TextUtils;
  32.  
  33. if (!TextUtils.isEmpty(str)) {
  34. ...
  35. }
  36.  
  37. Strings.nullToEmpty(str).isEmpty();
  38. // or
  39. Strings.isNullOrEmpty(str);
  40.  
  41. Strings.nullToEmpty(str).trim().isEmpty()
  42.  
  43. str != null && str.length() != 0
  44.  
  45. str != null && !str.equals("")
  46.  
  47. str != null && !"".equals(str)
  48.  
  49. import com.google.common.base.Strings;
  50.  
  51. if (!Strings.isNullOrEmpty(myString)) {
  52. return myString;
  53. }
  54.  
  55. if(str!= null && str.length() != 0 )
  56.  
  57. StringUtils.isNotBlank(str)
  58.  
  59. public class ALittleCleaner {
  60.  
  61. public List<Employee> findEmployees(String str, int dep) throws ClassNotFoundException, SQLException {
  62. log("List IN");
  63. List<Employee> list = Lists.newArrayList();
  64.  
  65. Connection con = getConnection();
  66. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  67. String qry = buildQueryString(str, dep);
  68. log(qry);
  69. ResultSet rs = stmt.executeQuery(qry);
  70. parseResults(list, rs);
  71. log("List Out");
  72. return list;
  73. }
  74.  
  75. private void parseResults(List<Employee> list, ResultSet rs) throws SQLException {
  76. while (rs.next()) {
  77. Employee employee = new Employee();
  78. String name = rs.getString(2);
  79. employee.setName(name);
  80. int id = rs.getInt(1);
  81. employee.setId(id);
  82. int dept = rs.getInt(4);
  83. employee.setDept(dept);
  84. int age = rs.getInt(3);
  85. employee.setAge(age);
  86. list.add(employee);
  87. }
  88. }
  89.  
  90. private String buildQueryString(String str, int dep) {
  91. String qry = "SELECT * FROM PERSON ";
  92. StringBuilder sb = new StringBuilder();
  93.  
  94. if (StringUtils.isNotBlank(str)) {
  95.  
  96. sb.append("WHERE NAME LIKE '%").append(str).append("%'");
  97. log(qry);
  98. }
  99. if (dep != 0) {
  100.  
  101. if (sb.toString().length() > 0) {
  102. sb.append(" AND ");
  103. } else {
  104. sb.append("WHERE ");
  105. }
  106. sb.append("dept=").append(dep);
  107. }
  108.  
  109. qry += sb.append(";").toString();
  110. return qry;
  111. }
  112.  
  113. private Connection getConnection() throws SQLException, ClassNotFoundException {
  114. Class.forName("com.mysql.jdbc.Driver");
  115.  
  116. String url = "jdbc:mysql://localhost:3306/general";
  117.  
  118. java.sql.Connection con = DriverManager.getConnection(url, "root", "1234");
  119.  
  120. log("URL: " + url);
  121. log("Connection: " + con);
  122. return con;
  123. }
  124.  
  125. private void log(String out) {
  126. // Replace me with a real logger
  127.  
  128. System.out.println(out);
  129.  
  130. }
  131.  
  132. class Employee implements Serializable {
  133. private static final long serialVersionUID = -8857510821322850260L;
  134. String name;
  135. int id, dept, age;
  136.  
  137. public String getName() {
  138. return this.name;
  139. }
  140.  
  141. public void setName(String name) {
  142. this.name = name;
  143. }
  144.  
  145. public int getId() {
  146. return this.id;
  147. }
  148.  
  149. public void setId(int id) {
  150. this.id = id;
  151. }
  152.  
  153. public int getDept() {
  154. return this.dept;
  155. }
  156.  
  157. public void setDept(int dept) {
  158. this.dept = dept;
  159. }
  160.  
  161. public int getAge() {
  162. return this.age;
  163. }
  164.  
  165. public void setAge(int age) {
  166. this.age = age;
  167. }
  168. }
  169.  
  170. }
  171.  
  172. /**
  173. * <p>Checks if a String is whitespace, empty ("") or null.</p>
  174. *
  175. * <pre>
  176. * StringUtils.isBlank(null) = true
  177. * StringUtils.isBlank("") = true
  178. * StringUtils.isBlank(" ") = true
  179. * StringUtils.isBlank("bob") = false
  180. * StringUtils.isBlank(" bob ") = false
  181. * </pre>
  182. *
  183. * @param str the String to check, may be null
  184. * @return <code>true</code> if the String is null, empty or whitespace
  185. * @since 2.0
  186. */
  187. public static boolean isBlank(String str) {
  188. int strLen;
  189. if (str == null || (strLen = str.length()) == 0) {
  190. return true;
  191. }
  192. for (int i = 0; i < strLen; i++) {
  193. if ((Character.isWhitespace(str.charAt(i)) == false)) {
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199.  
  200. if (!"mystring".equals(str)) {
  201. /* your code here */
  202. }
  203.  
  204. public List<Employee> findEmployees(String str, int dep) {
  205. Preconditions.checkState(StringUtils.isNotBlank(str), "Invalid input, input is blank or null");
  206. /** code here **/
  207. }
  208.  
  209. if(!"".equals(str) && str != null) {
  210. // do stuff.
  211. }
  212.  
  213. public class StringUtils{
  214.  
  215. public static boolean areSet(String... strings)
  216. {
  217. for(String s : strings)
  218. if(s == null || s.isEmpty)
  219. return false;
  220.  
  221. return true;
  222. }
  223.  
  224. }
  225.  
  226. if(!StringUtils.areSet(firstName,lastName,address)
  227. {
  228. //do something
  229. }
  230.  
  231. if(null != str && !str.isEmpty())
  232.  
  233. Optional.ofNullable(str)
  234. .filter(s -> !(s.trim().isEmpty()))
  235. .ifPresent(result -> {
  236. // your query setup goes here
  237. });
  238.  
  239. String str1 = "";
  240. String str2 = null;
  241.  
  242. if(StringUtils.isEmpty(str)){
  243. System.out.println("str1 is null or empty");
  244. }
  245.  
  246. if(StringUtils.isEmpty(str2)){
  247. System.out.println("str2 is null or empty");
  248. }
  249.  
  250. import com.google.common.base.Strings;
  251. import org.apache.commons.lang.StringUtils;
  252.  
  253. /**
  254. * Created by hu0983 on 2016.01.13..
  255. */
  256. public class StringNotEmptyTesting {
  257. public static void main(String[] args){
  258. String a = " ";
  259. String b = "";
  260. String c=null;
  261.  
  262. System.out.println("Apache:");
  263. if(!StringUtils.isNotBlank(a)){
  264. System.out.println(" a is blank");
  265. }
  266. if(!StringUtils.isNotBlank(b)){
  267. System.out.println(" b is blank");
  268. }
  269. if(!StringUtils.isNotBlank(c)){
  270. System.out.println(" c is blank");
  271. }
  272. System.out.println("Google:");
  273.  
  274. if(Strings.isNullOrEmpty(Strings.emptyToNull(a))){
  275. System.out.println(" a is NullOrEmpty");
  276. }
  277. if(Strings.isNullOrEmpty(b)){
  278. System.out.println(" b is NullOrEmpty");
  279. }
  280. if(Strings.isNullOrEmpty(c)){
  281. System.out.println(" c is NullOrEmpty");
  282. }
  283. }
  284. }
  285.  
  286. private boolean stringNotEmptyOrNull(String st) {
  287. return st != null && !st.isEmpty();
  288. }
  289.  
  290. org.springframework.util.StringUtils.hasLength(String str)
  291.  
  292. org.springframework.util.StringUtils.hasText(String str)
  293.  
  294. if(user==null) {
  295.  
  296. }else if(user.isEmpty()) {
  297.  
  298. }else{
  299. //code to run
  300. }
  301.  
  302. if(user==null) {
  303.  
  304. }else if(user.length() == 0) {
  305.  
  306. }else{
  307. //code to run
  308. }
  309.  
  310. if(user==null) {
  311.  
  312. }else if(user.equals("")) {
  313.  
  314. }else{
  315. //code to run
  316. }
  317.  
  318. String str="Nasir";
  319.  
  320. if(str.isEmpty()){
  321. System.out.println("string is empty");
  322. }
  323. else{
  324. System.out.println("string is not empty");
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement