-2

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?

Alla Sasikanth
  • 541
  • 3
  • 7
  • 22
  • 1
    If you just need the `.class`, then just use `".class"`. I assume you wanted something else? Can you define it more precisely? Show what your output string should be? – RealSkeptic Jan 16 '17 at 16:45
  • I'd suggest regex but I don't get your requirements. What would the `.class` part be? If it's just the literal then what do you mean by "extract"? – Thomas Jan 16 '17 at 16:45
  • It is just a pattern I am trying to extract from the string. – Alla Sasikanth Jan 16 '17 at 16:46
  • How should the output look like? – jengeb Jan 16 '17 at 16:47
  • do you want everything up to the .class? after the .class? what? – RAZ_Muh_Taz Jan 16 '17 at 16:47
  • Please provide more and better examples of that pattern. Would the string be something like `"yadda yadda ...\nWhatever.class file will be generated..."` and you want to extract `Whatever`? – Thomas Jan 16 '17 at 16:48
  • The output should be " `.class` " alone. – Alla Sasikanth Jan 16 '17 at 16:48
  • Then you don't need the original string and you don't need to "extract" anything. If you want `.class`, use `System.out.println(".class")`. Otherwise, explain what you want in your output that has anything to do with your input. – RealSkeptic Jan 16 '17 at 16:49
  • How about `String output = ".class";`? I know that sounds silly and is probably not what you are looking for _but we really don't know what it is that you want_. – Thomas Jan 16 '17 at 16:49
  • If output should be `.class`, then write `String output = ".class";` and you're done. – Andreas Jan 16 '17 at 16:50
  • @SasiKanth As you can see from previous comments, your description of what you want is extremely badly worded. Perhaps you should read: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Andreas Jan 16 '17 at 16:51

3 Answers3

0

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
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

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.

0

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));
}
Community
  • 1
  • 1
DDoomUs
  • 183
  • 9
  • People really want to help you, but as the comments indicate, your question is not clear. I'll try to restate what I think you're asking: I have a `String` variable which has the following structure in Java. String s = "hello\nthis is java programme\n.class file will be generated after executing it\n"; Now I need to extract the `.class` part from the above string variable so that I have a variable with only the class and the .class portion of the string. How do I extract only the classname and .class bit of the String? – DDoomUs Jan 16 '17 at 18:31