QnA | How to split String if it contains period symbol (.) in between?

How to split String if it contains period symbol (.) in between?

I have to write a program which takes file name with any extension as a parameter and it should return the extension of that file.
For Example:
If input parameters are as:
input_1 = testdata.properties
input_2 = userList.xlsx

Output would be:
output_1 = .properties
output_2 = .xlsx

Code Snippet:
Currently, I am using split() function with the regular expression (“.”), of String class to achieve this as mentioned below:

String fileExt = getExtenstion(“testdata.properties”);
System.out.println(“Extension of File : “+fileExt);

public String getExtension(String fileName)
String tempArray[] = fileName.split(“.”);
String fileExt = tempArray[1];
return fileExt;

When I executed this program I got, “ArrayIndexOutOfBoundsException” for yellow highlighted line of code as mentioned above.

Please help me out to get this code corrected. Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 51,167
  • Total page view: 70,434

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.