Knowee
Questions
Features
Study Tools

Write a Java program to check whether a substring appears before a period(.) within a given string. Sample Output:The given strings is: testabc.testIs 'abc' appear before period? trueThe given string is: test.abctestIs 'abc' appear before period? false

Question

Write a Java program to check whether a substring appears before a period(.) within a given string. Sample Output:The given strings is: testabc.testIs 'abc' appear before period? trueThe given string is: test.abctestIs 'abc' appear before period? false

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that checks whether a substring appears before a period in a given string.

public class Main {
    public static void main(String[] args) {
        checkSubstring("testabc.test", "abc");
        checkSubstring("test.abctest", "abc");
    }

    public static void checkSubstring(String str, String sub) {
        int periodIndex = str.indexOf(".");
        int subIndex = str.indexOf(sub);

        if (subIndex != -1 && periodIndex != -1 && subIndex < periodIndex) {
            System.out.println("The given string is: " + str);
            System.out.println("Is '" + sub + "' appear before period? true");
        } else {
            System.out.println("The given string is: " + str);
            System.out.println("Is '" + sub + "' appear before period? false");
        }
    }
}

This program defines a method checkSubstring that takes a string and a substring as parameters. It finds the index of the period and the substring in the string using the indexOf method. If both indices are not -1 (which means both the period and the substring exist in the string) and the index of the substring is less than the index of the period (which means the substring appears before the period), it prints that the substring appears before the period. Otherwise, it prints that the substring does not appear before the period. The main method tests this method with two example strings.

This problem has been solved

Similar Questions

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()

How to find all occurrences of a character or substring in Strings in Java?

Write a program to find the substring of a given string.

How can you find out if a sub-string is present in a given string?myString.includes(subString)myString.find(subString)myString.has(subString)

Given a string ‘S’ find whether the given string is periodic or not.A string is said to be periodic if it repeats itself after a certain number of characters and the period of repetition is less than the size of the string.For example: Let ‘S’ be “ababab” we can clearly see that this string is periodic as ‘ab’ is repeated 3 times to make the string ‘S’.

1/1

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.