Search code examples
javaregexstringstrip

How to strip off a string from a string in java?


I am writing a method that accepts a string which is only java attribute/field declaration and returns only the attribute name. e.g.:

private double d = 1000;
private boolean b;
private final int f = 100;
private char c = 'c';

so if the parameter is one of the above the method should return only d, b, f or c. How is algorithms should be implemented. I have tried to use regex to strip the word after the types but it became very complicated. Can anyone give me some clues, thanks


Solution

  • try this

        String type = str.replaceAll(".*\\s(.)\\s*=.*", "$1");