/* Java Search String using indexOf Example This example shows how we can search a word within a String object using indexOf method. */
public class SearchStringExample {
public static void main(String[] args) { //declare a String object String strOrig = "Hello world Hello World"; /* To search a particular word in a given string use indexOf method. indexOf method. It returns a position index of a word within the string if found. Otherwise it returns -1. */ int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } /* we can also search a word after particular position using indexOf(String word, int position) method. */ int positionIndex = strOrig.indexOf("Hello",11); System.out.println("Index of Hello after 11 is " + positionIndex); /* Use lastIndexOf method to search a last occurrence of a word within string. */ int lastIndex = strOrig.lastIndexOf("Hello"); System.out.println("Last occurrence of Hello is at index " + lastIndex);
} }
/* Output of the program would be : Found Hello at index 0 Index of Hello after 11 is 12 Last occurrence of Hello is at index 12 */
/* Java Reverse String Array Example This Java Reverse String Array example shows how to find sort an array of String in Java using Arrays and Collections classes. */
public class ReverseStringArrayExample { public static void main(String args[]){ //String array String[] strDays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday"}; /* * There are basically two methods, one is to use temporary array and * manually loop through the elements of an Array and swap them or to use * Arrays and Collections classes. * * This example uses the second approach i.e. without temp variable. * */ //first create a list from String array List<String> list = Arrays.asList(strDays); //next, reverse the list using Collections.reverse method Collections.reverse(list); //next, convert the list back to String array strDays = (String[]) list.toArray(); System.out.println("String array reversed"); //print the reversed String array for(int i=0; i < strDays.length; i++){ System.out.println(strDays[i]); } }
}
/* Output of above given Java Reverse String Array example would be String array reversed Wednesday Tuesday Monday Sunday */
/* Java InputStream to String Example This Java InputStream to String example shows how to convert InputStream to String in Java. */ public class ConvertInputStreamToStringExample { public static void main(String args[]) throws IOException{ //get InputStream of a file InputStream is = new FileInputStream("c:/data.txt"); String strContent; /* * There are several way to convert InputStream to String. First is using * BufferedReader as given below. */ //Create BufferedReader object BufferedReader bReader = new BufferedReader(new InputStreamReader(is)); StringBuffer sbfFileContents = new StringBuffer(); String line = null; //read file line by line while( (line = bReader.readLine()) != null){ sbfFileContents.append(line); } //finally convert StringBuffer object to String! strContent = sbfFileContents.toString(); /* * Second and one liner approach is to use Scanner class. This is only supported * in Java 1.5 and higher version. */ strContent = new Scanner(is).useDelimiter("\\A").next(); } }
public static void main(String args[]){ //create new java.util.Date object Date date = new Date(); /* * To convert java.util.Date to String, use SimpleDateFormat class. */ /* * crate new SimpleDateFormat instance with desired date format. * We are going to use yyyy-mm-dd hh:mm:ss here. */ DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); //to convert Date to String, use format method of SimpleDateFormat class. String strDate = dateFormat.format(date); System.out.println("Date converted to String: " + strDate); } }
/* Output of above given java.util.Date to String example would be Date converted to String: 2011-17-10 11:17:50 */
/* Java Convert int Array To String Example This Java Convert int Array To String example shows how to find convert an array of int to a String in Java. */
import java.util.Arrays;
public class ConvertIntArrayToStringExample {
public static void main(String args[]){ //int array int[] intNumbers = new int[]{1, 2, 3, 4, 5}; /* * First approach is to loop through all elements of an int array * and append them to StringBuffer object one by one. At the end, * use toString method to convert it to String. */ //create new StringBuffer object StringBuffer sbfNumbers = new StringBuffer(); //define the separator you want in the string. This example uses space. String strSeparator = " "; if(intNumbers.length > 0){ //we do not want leading space for first element sbfNumbers.append(intNumbers[0]); /* * Loop through the elements of an int array. Please * note that loop starts from 1 not from 0 because we * already appended the first element without leading space.s */ for(int i=1; i < intNumbers.length; i++){ sbfNumbers.append(strSeparator).append(intNumbers[i]); } } System.out.println("int array converted to String using for loop"); //finally convert StringBuffer to String using toString method System.out.println(sbfNumbers.toString()); /* * Second options is to use Arrays class as given below. * Use Arrays.toString method to convert int array to String. * * However, it will return String like [1, 2, 3, 4, 5] */ String strNumbers = Arrays.toString(intNumbers); System.out.println("String generated from Arrays.toString method: " + strNumbers); //you can use replaceAll method to replace brackets and commas strNumbers = strNumbers.replaceAll(", ", strSeparator).replace("[", "").replace("]", ""); System.out.println("Final String: " + strNumbers); } }
/* Output of above given convert int array to String example would be int array converted to String using for loop 1 2 3 4 5 String generated from Arrays.toString method: [1, 2, 3, 4, 5] Final String: 1 2 3 4 5 */
/* Java Char Array To String Example This Java char array to String example shows how to convert char array to String in Java. */
public class CharArrayToStringExample { public static void main(String args[]){ //char array char[] charArray = new char[]{'J','a','v','a'}; /* * To convert char array to String in Java, use * String(Char[] ch) constructor of Java String class. */ String str = new String(charArray); System.out.println("Char array converted to String: " + str); } }
/* Output of above given char array to String example would be Char array converted to String: Java */