How to split String if it contains period symbol (.) in between?
Problem:
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.