Java Articles

Page 88 of 450

Creating String Object from certain part of a character Array in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 200 Views

Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Create string object from some part of a string using the following String constructor. Through this we are fetching substring “IN” from the character array.String str = new String(ch, 4, 2);Examplepublic class Demo {    public static void main(String[] args) {       char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};       String str = new String(ch, 4, 2);       System.out.println(str);    } }OutputIN

Read More

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

Samual Sam
Samual Sam
Updated on 11-Mar-2026 569 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);Examplepublic 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

Read More

Java Program to construct one String from another

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 541 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.Examplepublic 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

Check that the String does not contain certain characters in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ 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.");Exampleimport 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

Perform Bubble Sort on strings in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 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.Examplepublic 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

Format Calendar with String.format() in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

Firstly, consider an object with date value.Object arrObj[] = { "Date", Calendar.getInstance() };After that, use the String.format() method to format Calendar and display the date.The following is an example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String []args){       Object arrObj[] = { "Date", Calendar.getInstance() };       System.out.println("Formatting Date...");       System.out.println(String.format("%1$s = %2$tY %2$tm %2$te", arrObj));    } }OutputFormatting Date... Date = 2018 11 17

Read More

Right pad a string in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To right pad a string, use the String.format and set the spaces.String.format("%1$-" + 20 + "s", "demotext"));If you add 30 above, it will display the next string after 30 spaces from the beginning.String.format("%1$-" + 30 + "s", "demotext")The following is an example.Examplepublic class Demo {    public static void main(String []args){       System.out.print(String.format("%1$-" + 20 + "s", "demotext"));       System.out.println("Right padded!");    } }Outputdemotext Right padded!

Read More

Left pad a string in Java

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 943 Views

To left pad a string, use the String.format and set the spaces.String.format("|%20s|", "demotext")If you add 30 above, it will display the first string after 30 spaces from the beginning.String.format("|%30s|", "demotext")Examplepublic class Demo {    public static void main(String []args) {       System.out.print(String.format("|%20s|", "demotext"));       System.out.println("Left padded!");    } }Output| demotext|Left padded

Read More

Line Separator in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ Views

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "one" + System.lineSeparator() + "two";       System.out.println(str);    } }Outputone twoLet us see another example. On Linux based system, the program will work correctly.Examplepublic class Demo {    public static void main(String[] args) {       String str = System.lineSeparator();       System.out.println((int) str.charAt(0));    } }Output10

Read More

Remove newline, space and tab characters from a string in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 18K+ Views

To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll("[\t ]", "");Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()The following is the complete example.Examplepublic class Demo {    public static void main(String[] args) {       String originalStr = "Demo\tText";       System.out.println("Original String with tabs, spaces and newline: "+originalStr);       originalStr = originalStr.replaceAll("[\t ]", "");       System.out.println("String after removing tabs, spaces and new line: "+originalStr);    } }OutputOriginal String with tabs, spaces and newline: Demo Text String ...

Read More
Showing 871–880 of 4,498 articles
« Prev 1 86 87 88 89 90 450 Next »
Advertisements