Commit f3f7dd95 by patryk

apache-commons

parent 72e9f773
......@@ -12,4 +12,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package apacz;
import org.apache.commons.lang3.StringUtils;
public class PrzykladyString {
public static void main(String[] args) {
String[] a = {"Ala", "Ola", "", "Ewelina", " ", null};
String[] b = {"Ala", "Ola", "Ewelina"};
String[] c = {" ", "", null};
// Pisząc w "zwykłej Javie" gdy chcemy wypisać z tablicy a wszystkie napisy, które nie są puste
// trzeba uwzględnić możliwość, że wartość jest nullem
int ile = 0;
for (String s : a) {
if(s != null && !s.isBlank()) {
System.out.println(s);
ile++;
}
}
System.out.println("tyle: " + ile);
System.out.println();
// biblioteka Apache Commons Lang oferuje gotowe metody umieszczona w klasach narzędziowych
// np. dla stringów mamy klasę StringUtils.
ile = 0;
for (String s : a) {
if(StringUtils.isNotBlank(s)) {
System.out.println(s);
ile++;
}
}
System.out.println("tyle: " + ile);
System.out.println();
// operacje dla całych tablic (lub wartości podawanych po przecinku)
if(StringUtils.isNoneBlank(a)) {
System.out.println("W a są same dobre napisy");
} else {
System.out.println("W a są też pustaki");
}
if(StringUtils.isNoneBlank(b)) {
System.out.println("W b są same dobre napisy");
} else {
System.out.println("W b są też pustaki");
}
if(StringUtils.isAllBlank(c)) {
System.out.println("ccc");
}
}
}
package apacz;
import static org.apache.commons.lang3.StringUtils.isAllBlank;
import static org.apache.commons.lang3.StringUtils.isNoneBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
public class PrzykladyString2 {
public static void main(String[] args) {
String[] a = {"Ala", "Ola", "", "Ewelina", " ", null};
String[] b = {"Ala", "Ola", "Ewelina"};
String[] c = {" ", "", null};
// Pisząc w "zwykłej Javie" gdy chcemy wypisać z tablicy a wszystkie napisy, które nie są puste
// trzeba uwzględnić możliwość, że wartość jest nullem
int ile = 0;
for (String s : a) {
if(s != null && !s.isBlank()) {
System.out.println(s);
ile++;
}
}
System.out.println("tyle: " + ile);
System.out.println();
// biblioteka Apache Commons Lang oferuje gotowe metody umieszczona w klasach narzędziowych
// np. dla stringów mamy klasę StringUtils.
ile = 0;
for (String s : a) {
if(isNotBlank(s)) {
System.out.println(s);
ile++;
}
}
System.out.println("tyle: " + ile);
System.out.println();
// operacje dla całych tablic (lub wartości podawanych po przecinku)
if(isNoneBlank(a)) {
System.out.println("W a są same dobre napisy");
} else {
System.out.println("W a są też pustaki");
}
if(isNoneBlank(b)) {
System.out.println("W b są same dobre napisy");
} else {
System.out.println("W b są też pustaki");
}
if(isAllBlank(c)) {
System.out.println("ccc");
}
if(isNoneBlank("Ala", "Ola", "Ela")) {
System.out.println("nie musi być tablica");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment