java string

Selection Sort in String Program in Java

Write a program to input the names of 15 cities sort in descending order using selection sort technique. import java.util.Scanner; public class SelectionSortString { public static void main(String[] args) { String cities[] = new String[15]; Scanner sc = new Scanner(System.in); int l = cities.length; System.out.println(“Enter 15 cities name:”); for (int i = 0; i < […]

Java String Function replace()

The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence. Since JDK 1.5, a new replace() method is introduced, allowing you to replace a sequence of char values. Java String replace(char old, char new) method example public class ReplaceExample1{ public static void main(String args[]){ String […]

Java String Function length()

The java string length() method length of the string. It returns count of total number of characters. The length of java string is same as the unicode code units of the string. Java String length() method example public class LengthExample{ public static void main(String args[]){ String s1=”javatpoint”; String s2=”python”; System.out.println(“string length is: “+s1.length());//10 is the […]

Java String Function lastIndexOf()

The java string lastIndexOf() method returns last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero. Java String lastIndexOf() method example public class LastIndexOfExample{ public static void main(String args[]){ String s1=”this is index of example”;//there are 2 ‘s’ characters in this sentence […]

Java String Function join()

The java string join() method returns a string joined with given delimiter. In string join method, delimiter is copied for each elements. In case of null element, “null” is added. The join() method is included in java string since JDK 1.8. There are two types of join() methods in java string. Java String join() method […]

Java String Function isEmpty()

The java string isEmpty() method checks if this string is empty or not. It returns true, if length of string is 0 otherwise false. In other words, true is returned if string is empty otherwise it returns false. The isEmpty() method of String class is included in java string since JDK 1.6. Java String isEmpty() […]

Java String Function intern()

The java string intern() method returns the interned string. It returns the canonical representation of string. It can be used to return string from memory, if it is created by new keyword. It creates exact copy of heap string object in string constant pool. Java String intern() method example public class InternExample{ public static void […]

Java String Function getChars()

The java string getChars() method copies the content of this string into specified char array. There are 4 arguments passed in getChars() method. The signature of getChars() method is given below: Java String getChars() method example public class StringGetCharsExample{ public static void main(String args[]){ String str = new String(“hello efaculty how r u”); char[] ch […]

Java String Function getBytes()

The java string getBytes() method returns the byte array of the string. In other words, it returns sequence of bytes. Java String getBytes() method example public class StringGetBytesExample{ public static void main(String args[]){ String s1=”ABCDEFG”; byte[] barr=s1.getBytes(); for(int i=0;i < barr.length;i++){ System.out.println(barr[i]); } }} Output: 65 66 67 68 69 70 71

Java String Function format()

The java string format() method returns the formatted string by given locale, format and arguments. If you don’t specify the locale in String.format() method, it uses default locale by calling Locale.getDefault() method. The format() method of java language is like sprintf() function in c language and printf() method of java language. Java String format() method […]

1 2