Write a Java program to check whether a substring(abc) appears before a period(.) within a given string
Question
Write a Java program to check whether a substring(abc) appears before a period(.) within a given string
Solution 1
Sure, here is a simple Java program that checks if a substring "abc" appears before a period "." in a given string.
public class Main {
public static void main(String[] args) {
String str = "This is a test string abc. Let's see if it works.";
System.out.println(checkSubstringBeforePeriod(str));
}
public static boolean checkSubstringBeforePeriod(String str) {
int indexABC = str.indexOf("abc");
int indexPeriod = str.indexOf(".");
if (indexABC != -1 && indexPeriod != -1 && indexABC < indexPeriod) {
return true;
} else {
return false;
}
}
}
In this program, we have a function checkSubstringBeforePeriod that takes a string as input. It finds the index of the substring "abc" and the index of the period "." in the string using the indexOf method. If both "abc" and "." are present in the string and "abc" appears before ".", the function returns true. Otherwise, it returns false.
The main method is where the program starts. It defines a test string and calls the checkSubstringBeforePeriod function with the test string as argument. The result is then printed to the console.
Solution 2
Sure, here is a simple Java program that checks if the substring
Solution 3
Sure, here is a simple Java program that checks if a substring "abc" appears before a period "." in a given string.
public class Main {
public static void main(String[] args) {
String str = "abc.def";
System.out.println(checkSubstringBeforePeriod(str));
}
public static boolean checkSubstringBeforePeriod(String str) {
int indexABC = str.indexOf("abc");
int indexPeriod = str.indexOf(".");
if (indexABC != -1 && indexPeriod != -1 && indexABC < indexPeriod) {
return true;
} else {
return false;
}
}
}
In this program, we have a function checkSubstringBeforePeriod that takes a string as input. It finds the index of the substring "abc" and the index of the period "." in the string using the indexOf method. If both "abc" and "." are present in the string and "abc" appears before ".", the function returns true. Otherwise, it returns false.
The main method is the entry point of the program. It defines a string and calls the checkSubstringBeforePeriod function with this string, then prints the result.
Solution 4
Sure, here is a simple Java program that checks if a substring "abc" appears before a period "." in a given string.
public class Main {
public static void main(String[] args) {
String str = "This is a test string abc. This is another test string.";
System.out.println(checkSubstringBeforePeriod(str, "abc"));
}
public static boolean checkSubstringBeforePeriod(String str, String substring) {
int indexSubstring = str.indexOf(substring);
int indexPeriod = str.indexOf(".");
if (indexSubstring == -1 || indexPeriod == -1) {
return false;
} else {
return indexSubstring < indexPeriod;
}
}
}
In this program, we have a function checkSubstringBeforePeriod that takes a string and a substring as input. It finds the index of the substring and the period in the string using the indexOf method. If either the substring or the period is not found (i.e., indexOf returns -1), the function returns false. Otherwise, it checks if the index of the substring is less than the index of the period, which means the substring appears before the period. If so, it returns true; otherwise, it returns false.
The main method tests this function with a sample string and the substring "abc".
Similar Questions
Write a program to find the substring of a given string.
How to find all occurrences of a character or substring in Strings in Java?
Which method is used to check if a string starts with a specific substring in Java?Question 2Answera.startsWith()b.beginsWith()c.firstWith()d.initiateWith()
This exercise is to test your understanding of Java Strings. A sample String declaration:String myString = "Hello World!"The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.Given two strings of lowercase English letters, and , perform the following operations:Sum the lengths of and .Determine if is lexicographically larger than (i.e.: does come before in the dictionary?).Capitalize the first letter in and and print them on a single line, separated by a space.Input FormatThe first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.Output FormatThere are three lines of output:For the first line, sum the lengths of and .For the second line, write Yes if is lexicographically greater than otherwise print No instead.For the third line, capitalize the first letter in both and and print them on a single line, separated by a space.Sample Input 0hellojavaSample Output 09NoHello JavaExplanation 0String is "hello" and is "java". has a length of , and has a length of ; the sum of their lengths is .When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than and the answer is No.When you capitalize the first letter of both and and then print them separated by a space, you get "Hello Java".
How can you find out if a sub-string is present in a given string?myString.includes(subString)myString.find(subString)myString.has(subString)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.