Found 34494 Articles for Programming

Perform Bubble Sort on strings in Java

Samual Sam
Updated on 27-Jun-2020 06:18:25

2K+ Views

To perform Bubble Sort, try the below given code. In this each each pair of adjacent elements is compared and the elements are swapped if they are not in order.The following is an example.Example Live Demopublic class Demo {    public static void main(String []args) {       String str[] = { "s", "k", "r", "v", "n"};       String temp;       System.out.println("Sorted string...");       for (int j = 0; j < str.length; j++) {          for (int i = j + 1; i < str.length; i++) {         ... Read More

Java Program to locate a substring in a string

karthikeya Boyini
Updated on 18-Jun-2024 18:39:39

14K+ Views

To locate a substring in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a substring ‘demo’ in a string and get the index.int index = str.indexOf( 'demo');Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "testdemo";       System.out.println("String: "+str);       int index = str.indexOf("demo");       System.out.printf("Substring 'demo' is at index %d", index);    } }OutputString: testdemo Substring 'demo' is at index 4

Java Program to locate a character in a string

Samual Sam
Updated on 21-Jun-2024 11:27:10

13K+ Views

To locate a character in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a character ‘d’ in a string and get the index.int index = str.indexOf( 'd');Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "testdemo";       System.out.println("String: "+str);       int index = str.indexOf( 'd' );       System.out.printf("'d' is at index %d, index);    } }OutputString: testdemo 'd' is at index 4Let us see another example. The method returns -1, if the character isn’t found −Example Live Demopublic class ... Read More

Check that the String does not contain certain characters in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:09:03

3K+ Views

Let’s say the following is our string with special characters.String str = "test*$demo";Check for the special characters.Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find();Now, if the bool value “val” is true, that would mean the special characters are in the string.if (val == true) System.out.println("Special characters are in the string.");Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String []args) {       String str = "test*$demo";       System.out.println("String: "+str);       Pattern pattern = Pattern.compile("[^A-Za-z0-9]");       Matcher match = pattern.matcher(str);       boolean val ... Read More

Check whether the String contains only digit characters in Java

Samual Sam
Updated on 27-Jun-2020 06:10:48

953 Views

The following is our string.String str = "4434";To check whether the above string has only digit characters, try the following if condition that uses matches() method and checks for every character.if(str.matches("[0-9]+") && str.length() > 2) { System.out.println("String contains only digits!"); }Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "4434";       if(str.matches("[0-9]+") && str.length() > 2) {          System.out.println("String contains only digits!");       }    } }OutputString contains only digits!Let us see another example.Example Live Demopublic class Demo {    public static void main(String []args) ... Read More

Delete all whitespaces from a String in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:01:21

103 Views

To delete all whitespaces from a string, use the replaceAll() method and replace every whitespace with empty.Let’s say the following is our string.String str = "This is it!";Now, let us replace the whitespaces that will eventually delete them.String res = str.replaceAll("\s+","");Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "This is it!";       System.out.println("String: "+str);       String res = str.replaceAll("\s+","");       System.out.println("String after deleting whitespace: "+res);    } }OutputString: This is it! String after deleting whitespace: Thisisit!

Java Program to swap the case of a String

Samual Sam
Updated on 18-Jun-2024 18:01:46

12K+ Views

To swap the case of a string, use.toLowerCase() for title case string. toLowerCase() for uppercase stringtoUpperCase() for lowercase stringLoop through what we discussed above for all the characters in the sting.for (int i = 0; i > len; i++) {    c = str.charAt(i);    // title case converted to lower case    if (Character.isTitleCase(c)) {       c = Character.toLowerCase(c);    }    // upper case converted to lower case    if (Character.isUpperCase(c)) {       c = Character.toLowerCase(c);    }    // lower case converted to upper case    if (Character.isLowerCase(c)) {       c ... Read More

Java Program to construct one String from another

karthikeya Boyini
Updated on 27-Jun-2020 05:44:55

412 Views

To construct on string from another, firstly take a charcter array for the first string.char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch);The above forms first string. Now, let us created another string from the first string.String str2 = new String(str1);In this way, we can easily construct one string from another.Example Live Demopublic class Demo {    public static void main(String[] args) {       char ch[] = { 'A', 'M', 'I', 'T' };       String str1 = new String(ch);       String str2 = new String(str1);       String str3 ... Read More

Extracting a substring as an array of characters in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:47:28

3K+ Views

To extract a substring as an array of characters in Java, use the getChars() method.Let’s say the following is our string and character array.String str = "World is not enough!"; char[] chArr = new char[10];Now, use the getChars() method to extract a substring.str.getChars(13, 19, chArr, 0);The above substring is an array of characters which can be displayed as shown in the complete example below −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "World is not enough!";       char[] chArr = new char[10];       str.getChars(13, 19, chArr, ... Read More

Creating a string from a subset of the array elements in Java

Samual Sam
Updated on 27-Jun-2020 05:48:09

349 Views

To get a string from a subset of the character array elements, use the copyValueOf() method. This method returns a String that represents the character sequence in the array specified.Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Now, let us create a string from the subset of the above array elements.String str = String.copyValueOf(ch, 4, 2);Example Live Demopublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};       String str = String.copyValueOf(ch, 4, 2);       System.out.println(str);    } }OutputIN

Advertisements