SUB STRINGS FROM A GIVEN STRING

 

Hello Shouters!! Congratulations! 🙌 Today we will learn the most basic program that is Java Program To get substrings from a given string.

Strings hold sequences of characters. We’ve already seen instances of a String, for example when you printed out "Hello World".

https://www.shoutcoders.com/java-program-to-print-hello-world/
Check this one at first then continue.

Java is a pure Object Oriented Programming language and for running program. You have to make a class.

A very simple declaring of a string with a primitive, we declare the variable by specifying the type first:

String greeting = "Hello World";

As a beginner, it is very easy to learn some of the string methods that come with the String class:

length()
concat()
indexOf()
charAt()
equals() / equalsIgnoreCase()
substring()
toUpperCase() / toLowerCase()

Feel free to play around with these string methods.🙌

.

According to the study, many responsible websites prefer this approach. (Given below)

// Get substrings from a string
import java.util.Scanner;
class SC{
	static void SUBSTR(String str)
	{
		int len=str.length();
		//System.out.println(len);
		for(int i=1; i<=len; i++)
		{
		// locate string range
			for(int j=0; j<= len-i ; j++)
			{   
		// Locate String starting Point 
				int k=j+i-1;	
				for(int m=j; m<=k; m++)
				{
				System.out.print(str.charAt(m));
// charAt use for access character using index value as parameter.
				} // m
			System.out.println();
			} // j			
		} // i
	}//method


	// main method
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// Used for Scanning 

		System.out.println("Enter your String: ");
		String val=sc.next();
		// next() used for String type value

		System.out.println("Entered string is: "+val);
		System.out.println("Substrings are given below ");
		

		SUBSTR(val);
		// call static method
	} 
}// end of class
Output: 
Enter your String:
abc
Entered string is: abc
Substrings are given below
a
b
c
ab
bc
abc

.

Our study indicates that an incrementing number of people triggering some other approach. Given below is to get a substring from a string.

There may be times when we only want a part of a string. In such cases, we may want to extract a substring from a string.

The substring() method does exactly that. For example:

String line = "The Heav'ns and all the Constellations rung";

System.out.println(line.substring(23));

It would output "Constellations rung" because that’s what begins at index 23 and ends at the end of line. The substring begins with the character at the specified index and extends to the end of the string.

But suppose we want a substring at the middle of the string. We can include two arguments with this string method. For example:

String line = "The Heav'ns and all the Constellations rung";

System.out.println(line.substring(23, 38));

It would output "Constellations" because that’s the substring that begins at index 23 and ends at index 38.

.

Another new approach mentions below helps you to memorize with Less effort, Less code, with professional skills.

import java.util.Scanner;
// for scanning purpose
class SC { 
// Function to print all substring 
public static void SUBSTR(String str) 
{ 
int len= str.length();
for (int i = 0; i < len; i++)  // use as a starting index
	for (int j = i+1; j <= len; j++) // use as an ending index
  		System.out.println(str.substring(i, j)); 
} 

public static void main(String[] args) 
{ 
	Scanner sc=new Scanner(System.in);
// for scanning purpose
	System.out.print("Enter the string: ");	
	String str = sc.next(); 
	SUBSTR(str); 
// calling method
} 
} 
// values are not sorted.
Output:
Enter the string: abc
a
ab
abc
b
bc
c

Try to run Java Program on your Personal Computer with the methods given above.

.

If you have any doubt regarding this feel free to comment below and if you want to add any questions. Write your suggestion below in the comment box.

Comments

Popular posts from this blog

Python Program to Find HCF or GCD

SAME NAME IN CLASS METHOD AND CONSTRUCTOR

CONVERT CARTESIAN COORDINATES TO POLAR COORDINATE