string method example in java
Java
// all String methods
public class StringMethodsDemo {
public static void main(String[] args) {
String targetString = "Java is fun to learn";
String s1= "JAVA";
String s2= "Java";
String s3 = " Hello Java ";
System.out.println("Char at index 2(third position): " + targetString.charAt(2));
System.out.println("After Concat: "+ targetString.concat("-Enjoy-"));
System.out.println("Checking equals ignoring case: " +s2.equalsIgnoreCase(s1));
System.out.println("Checking equals with case: " +s2.equals(s1));
System.out.println("Checking Length: "+ targetString.length());
System.out.println("Replace function: "+ targetString.replace("fun", "easy"));
System.out.println("SubString of targetString: "+ targetString.substring(8));
System.out.println("SubString of targetString: "+ targetString.substring(8, 12));
System.out.println("Converting to lower case: "+ targetString.toLowerCase());
System.out.println("Converting to upper case: "+ targetString.toUpperCase());
System.out.println("Triming string: " + s3.trim());
System.out.println("searching s1 in targetString: " + targetString.contains(s1));
System.out.println("searching s2 in targetString: " + targetString.contains(s2));
char [] charArray = s2.toCharArray();
System.out.println("Size of char array: " + charArray.length);
System.out.println("Printing last element of array: " + charArray[3]);
}
}
public String trim()
String x = " hi ";
System.out.println( x + "x" ); // result is" hi x"
System.out.println(x.trim() + "x"); // result is "hix"
public String substring(int begin)/ public String substring(int begin, int end)
------------------------------------------------------------------------------
String x = "0123456789"; // the value of each char is the same as its index!
System.out.println( x.substring(5) ); // output is "56789"
System.out.println( x.substring(5, 8)); // output is "567"
public boolean equalsIgnoreCase(String s)
------------------------------------------
String x = "Exit";
System.out.println( x.equalsIgnoreCase("EXIT")); // is "true"
System.out.println( x.equalsIgnoreCase("tixe")); // is "false"
public char charAt(int index)
-----------------------------
String x = "airplane";
System.out.println( x.charAt(2) ); // output is 'r'
public boolean contains(“searchString”)
String x = “Java is programming language”;
System.out.println(x.contains(“Amit”)); // This will print false
System.out.println(x.contains(“Java”)); // This will print true
public String toUpperCase()
-------------------------------
String x = "A New Java Book";
System.out.println( x.toUpperCase() ); // output is"A NEW JAVA BOOK"
public int length()
-----------------------
String x = "01234567";
System.out.println( x.length() ); // returns "8"
public String concat(String s)
String x = "book";
System.out.println( x.concat(" author") ); // output is "book author"
Also in Java:
- Title
- edit a txt file using java
- Category
- Java
- Title
- java check if file exists
- Category
- Java
- Title
- java io
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- Manifest merger failed androidx
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- get file path java
- Category
- Java
- Title
- How do you count characters in a string array in Java?
- Category
- Java
- Title
- java jpa criteriabuilder in xample
- Category
- Java
- Title
- arraylist with values java
- Category
- Java
- Title
- treemap get order java
- Category
- Java
- Title
- java test if a string is a int
- Category
- Java
- Title
- Gson write json to file
- Category
- Java
- Title
- remove first character from string
- Category
- Java
- Title
- nullpointer extension
- Category
- Java
- Title
- sum of arraylist java 8
- Category
- Java
- Title
- compareto method java
- Category
- Java
- Title
- working with buttons in applet java
- Category
- Java
- Title
- how to create a java jframe
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- map multiple fields from java stream
- Category
- Java
- Title
- armstrong number in java
- Category
- Java
- Title
- joptionpane fonctionnement java
- Category
- Java
- Title
- reverse a string in java
- Category
- Java
- Title
- java time code
- Category
- Java
- Title
- java int array
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- hello world in java
- Category
- Java
- Title
- html top padding
- Category
- Java
- Title
- java vector push_back
- Category
- Java
- Title
- html redirect
- Category
- Java
- Title
- how to create java jframe in eclipse
- Category
- Java
- Title
- what is enum in java
- Category
- Java
- Title
- spring-boot actuator not working
- Category
- Java
- Title
- graal.js pass javascript array to java function
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- java function that returns the index of the largest value in an array
- Category
- Java
- Title
- java for loop add multiple declarations
- Category
- Java
- Title
- method overloading
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- inputstream to string java
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- java is power of 2
- Category
- Java
- Title
- java foreach map
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- localdate to string java
- Category
- Java
- Title
- how to iterate hashset in java 8
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- java connect to mysql
- Category
- Java
- Title
- string reverse in java
- Category
- Java
- Title
- java script snippet for responsive
- Category
- Java
- Title
- spigot custom mob
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- transformer un string en double java
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- java string contains char
- Category
- Java
- Title
- how to read a csv file java
- Category
- Java
- Title
- import java.util.*;
- Category
- Java
- Title
- how to extract data from delimited text file in java
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- java shuffle list
- Category
- Java
- Title
- load contents of file into string java
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- number of matches regex java
- Category
- Java
- Title
- set html text android java
- Category
- Java
- Title
- D/NetworkSecurityConfig: No Network Security Config specified, using platform default
- Category
- Java
- Title
- java stack empty
- Category
- Java
- Title
- spigot get player from UUID
- Category
- Java
- Title
- spigot deal damage
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- java classes and methods simple logic with comments
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- best companies to workk in
- Category
- Java
- Title
- get drawable with string android java
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- java gateway process exited before sending its port number pyspark anaconda
- Category
- Java
- Title
- new hashmap java
- Category
- Java
- Title
- imageview.setbackground
- Category
- Java
- Title
- check if user has internet connection in kotlin
- Category
- Java
- Title
- main method java
- Category
- Java
- Title
- indexof java
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- math max java
- Category
- Java
- Title
- check if value exists in hashset java
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- find length of array java
- Category
- Java
- Title
- Java use Base64
- Category
- Java
- Title
- how to check if an arraylist contains a value in java recursion
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- split arraylist items java
- Category
- Java
- Title
- scanner in java
- Category
- Java
- Title
- how to iterate hashmap java
- Category
- Java
- Title
- java jagged array days and months
- Category
- Java
- Title
- java define custom exception
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java