I think you often use trim
and contains
.
1, contains
Search if a specific character string is included in the character string.
Lowercase and uppercase </ font>.
True
if included.
False
if not included.
StringSearch.java
public class StringSearch {
public static void main(String[] args) {
String JAVA = "Java";
String RUBY = "Ruby";
String SOFTWARE = "Software";
String word1 = "I study Java";
String word2 = "I made app made by Ruby on rails";
String word3 = "I want to be Software developer";
boolean javaValid = word1.contains(JAVA);
boolean rubyValid = word2.contains(RUBY);
boolean softwareValid = word3.contains(SOFTWARE);
System.out.println("\"I study Java\".contains(\"Java\") : "+javaValid);
System.out.println("\"I made app made by Ruby on rails\".contains(\"Ruby\") : "+rubyValid);
System.out.println("\"I want to be Software developer\".contains(\"Software\") : "+softwareValid);
boolean result = word1.contains("java");
System.out.println("\"Java\".contains(\"java\") : "+result);
}
}
result.java
"I study Java".contains("Java") : true
"I made app made by Ruby on rails".contains("Ruby") : true
"I want to be Software developer".contains("Software") : true
"Java".contains("java") : false
2, startsWith
Determine if a string starts with a particular string.
Lowercase and uppercase </ font>.
Space is also judged as the first character </ font>.
True
if it starts.
False
if it doesn't start.
sample.java
String word4 = "JavaScript";
String word5 = " JavaScript";
boolean starts1 = word4.startsWith("J");
boolean starts2 = word4.startsWith("j");
boolean startsWithSpace1 = word5.startsWith("J");
boolean startsWithSpace2 = word5.startsWith(" ");
System.out.println("\"JavaScript\".startsWith(\"J\") : " + starts1);
System.out.println("\"JavaScript\".startsWith(\"j\") : " + starts2);
System.out.println("\" JavaScript\".startsWith(\"J\") : " + startsWithSpace1);
System.out.println("\" JavaScript\".startsWith(\" \") : " + startsWithSpace2);
result.java
"JavaScript".startsWith("J") : true
"JavaScript".startsWith("j") : false
" JavaScript".startsWith("J") : false
" JavaScript".startsWith(" ") : true
3, endsWith
Determine if a string ends with a particular string.
Lowercase and uppercase </ font>.
Space is also judged as the last character </ font>.
True
when finished.
If it doesn't end, false
.
sample.java
String word4 = "JavaScript";
String word5 = " JavaScript ";
boolean end1 = word4.endsWith("t");
boolean end2 = word4.endsWith("T");
boolean endWithSpace1 = word5.endsWith("t");
boolean endWithSpace2 = word5.endsWith(" ");
System.out.println("\"JavaScript\".endsWith(\"t\") : " + end1);
System.out.println("\"JavaScript\".endsWith(\"T\") : " + end2);
System.out.println("\" JavaScript \".endsWith(\"t\") : " + endWithSpace1);
System.out.println("\" JavaScript \".endsWith(\" \") : " + endWithSpace2);
result.java
"JavaScript".endsWith("t") : true
"JavaScript".endsWith("T") : false
" JavaScript ".endsWith("t") : false
" JavaScript ".endsWith(" ") : true
1, toLowerCase If you use this method on a string, it will convert it to all lowercase. Passing Japanese does not cause an error.
sample.java
String onlyUpper = "JAVASCRIPT";
String onlyLower = "javascript";
String mixed = "JaVaScRiPt";
String onlyLowerWithSpace = "i love java";
String onlyUpperWithSpace = "I LOVE JAVA";
String mixedWithSpace = "I lOvE jAvA";
String japanese = "Hello. It's sunny today.";
String expectLower1 = onlyUpper.toLowerCase();
String expectLower2 = onlyLower.toLowerCase();
String expectLower3 = mixed.toLowerCase();
String expectLower4 = onlyLowerWithSpace.toLowerCase();
String expectLower5 = onlyUpperWithSpace.toLowerCase();
String expectLower6 = mixedWithSpace.toLowerCase();
String expectLower7 = japanese.toLowerCase();
System.out.println("\"JAVASCRIPT\".toLowerCase() => " + expectLower1);
System.out.println("\"javascript\".toLowerCase() => " + expectLower2);
System.out.println("\"JaVaScRiPt\".toLowerCase() => " + expectLower3);
System.out.println("\"i love java\".toLowerCase() => " + expectLower4);
System.out.println("\"I LOVE JAVA\".toLowerCase() => " + expectLower5);
System.out.println("\"I lOvE jAvA\".toLowerCase() => " + expectLower6);
System.out.println("\"Hello. It's sunny today.\".toLowerCase() => " + expectLower7);
result.java
"JAVASCRIPT".toLowerCase() => javascript
"javascript".toLowerCase() => javascript
"JaVaScRiPt".toLowerCase() => javascript
"i love java".toLowerCase() => i love java
"I LOVE JAVA".toLowerCase() => i love java
"I lOvE jAvA".toLowerCase() => i love java
"Hello. It's sunny today.".toLowerCase() => Hello. It's sunny today.
2, toUpperCase If you use this method on a string, it will convert it to all uppercase.
sample.java
String onlyUpper = "JAVASCRIPT";
String onlyLower = "javascript";
String mixed = "JaVaScRiPt";
String onlyLowerWithSpace = "i love java";
String onlyUpperWithSpace = "I LOVE JAVA";
String mixedWithSpace = "I lOvE jAvA";
String japanese = "Hello. It's sunny today.";
String expectUpper1 = onlyUpper.toUpperCase();
String expectUpper2 = onlyLower.toUpperCase();
String expectUpper3 = mixed.toUpperCase();
String expectUpper4 = onlyLowerWithSpace.toUpperCase();
String expectUpper5 = onlyUpperWithSpace.toUpperCase();
String expectUpper6 = mixedWithSpace.toUpperCase();
String expectUpper7 = japanese.toUpperCase();
System.out.println("\"JAVASCRIPT\".toUpperCase() => " + expectUpper1);
System.out.println("\"javascript\".toUpperCase() => " + expectUpper2);
System.out.println("\"JaVaScRiPt\".toUpperCase() => " + expectUpper3);
System.out.println("\"i love java\".toUpperCase() => " + expectUpper4);
System.out.println("\"I LOVE JAVA\".toUpperCase() => " + expectUpper5);
System.out.println("\"I lOvE jAvA\".toUpperCase() => " + expectUpper6);
System.out.println("\"Hello. It's sunny today.\".toUpperCase() => " + expectUpper7);
result.java
"JAVASCRIPT".toUpperCase() => JAVASCRIPT
"javascript".toUpperCase() => JAVASCRIPT
"JaVaScRiPt".toUpperCase() => JAVASCRIPT
"i love java".toUpperCase() => I LOVE JAVA
"I LOVE JAVA".toUpperCase() => I LOVE JAVA
"I lOvE jAvA".toUpperCase() => I LOVE JAVA
"Hello. It's sunny today.".toUpperCase() => Hello. It's sunny today.
3, trim Remove leading and trailing whitespace in a string. Note: whitespace is removed only before and after, not whitespace in the string. </ font>
sample.java
String trim1 = " Java and JavaScript ";
String trim2 = "Java and JavaScript";
String afterTrim1 = trim1.trim();
String afterTrim2 = trim2.trim();
System.out.println("\" Java and JavaScript \".length() => " + trim1.length());
System.out.println("\" Java and JavaScript \".trim().length() => " + afterTrim1.length());
System.out.println("\" Java and JavaScript \".trim() => " + afterTrim1);
System.out.println("\"Java and JavaScript\".length() => " + trim2.length());
System.out.println("\"Java and JavaScript\".trim().length() => " + afterTrim2.length());
System.out.println("\"Java and JavaScript\".trim() => " + afterTrim2);
result.java
" Java and JavaScript ".length() => 23
" Java and JavaScript ".trim().length() => 19
" Java and JavaScript ".trim() => Java and JavaScript
"Java and JavaScript".length() => 19
"Java and JavaScript".trim().length() => 19
"Java and JavaScript".trim() => Java and JavaScript
4, replace Replace a specific string contained in a string with an arbitrary string. Lowercase and uppercase </ font>.
sample.java
String beforeRep = "I like Java and Ruby, Python";
String afterRep1 = beforeRep.replace("Java", "Golang");
String afterRep2 = beforeRep.replace("java", "Golang");
System.out.println("\"I like Java and Ruby, Python\".replace(\"Java\", \"Golang\") => " + afterRep1);
System.out.println("\"I like Java and Ruby, Python\".replace(\"java\", \"Golang\") => " + afterRep2);
result.java
"I like Java and Ruby, Python".replace("Java", "Golang") => I like Golang and Ruby, Python
"I like Java and Ruby, Python".replace("java", "Golang") => I like Java and Ruby, Python
Recommended Posts