I have a String
variable as follows in Java.
String s = "hello\nthis is java programme\n.class file will be generated after executing it\n";
Now I need to exctract the .class
part from the above string variable. How to do that?
I have a String
variable as follows in Java.
String s = "hello\nthis is java programme\n.class file will be generated after executing it\n";
Now I need to exctract the .class
part from the above string variable. How to do that?
I really only see one way for you to not simply just output ".class" which is to first see if the string contains ".class" before printing it. Here is a function to do so. Pass a string to look for and a string to search for it.
//Returns the string if found, else returns an empty string
public String FindString(String whatToFind, String whereToFind)
{
return whereToFind.contains(whatToFind) ? whatToFind : "";
}
Output
String s = "hello\nthis is java programme\n.class file will be generated after executing it\n";
System.out.println(FindString(".class", s)); // prints .class
if you just want to check if the string has ".class" pattern inside you can easily just check it like:
s.contains(".class");
Or if you want check that the pattern like .class
inside your string that contains \n
using regex:
Pattern p = Pattern.compile(".*\\.class.*", Pattern.DOTALL);
Matcher m = p.matcher(s);
boolean b = m.matches();
DOTALL
enables \n
also considered as a character.
s is the String you defined in your code.
Use regular expressions, like in this answer
String s = "hello\nthis is java programme\n<some_class_name_here>.class file will be generated after executing it\n";
//the following pattern I think will find what you're looking for,
Pattern pattern = Pattern.compile("\n(.*\.class)");
Matcher matcher = pattern.matcher(s);
if (matcher.find())
{
System.out.println(matcher.group(1));
}