-5

I have this text:

1 A Maths 

And i would like to get only Maths.

I don´t know about regular expressions.

Can any one help me, please?

Thanks

  • 5
    Instead of asking for a fish, learn to fish. That's what programmers do. – Marko Topolnik Jan 27 '14 at 20:25
  • Why would you need regex for this? If you don't know about regular expressions, what is it that lead you to believe your problem is solved with them? Seriously, this question has no information on what it is you're really trying to do, other than saying that you want to get "Maths" from a string. – Mike Jan 27 '14 at 20:28
  • 2
    Your question is vague. If your string is always "1 A Maths", the easiest solution is to take the substring from char 4. Somehow I don't think that's what you want? – Reinstate Monica Jan 27 '14 at 20:28
  • @Mike I need it for using it in Android for getting a variable with the name of the subject and pass it to another Activity – user3229208 Jan 27 '14 at 20:30
  • @Boschman No of course, it´s variable, although always is a number, a blank space, a letter, a blank space and the number of the subject – user3229208 Jan 27 '14 at 20:31
  • If you were only allowed to use functions you already knew, what would you do? – Floris Jan 27 '14 at 20:32
  • string indexOf(" ") space, find the third iteration of that, then grab the substring from there to the end of the string – Mike Jan 27 '14 at 20:34
  • To get everything after the **last** whitespace character of a string: use `/\s(\S+)$/` ([example](http://regex101.com/r/fJ9pJ7)) – Sam Jan 27 '14 at 20:34
  • @SamSullivan I would like to vote for you – user3229208 Jan 27 '14 at 20:37
  • @user3229208 keep in mind that if there is a trailing space after the name, it will break the regex – Mike Jan 27 '14 at 20:38

1 Answers1

2

Why don't you split the string ?

String chain="A Maths";
String[] array=chain.split(" ");
String number=array[0];
String course=array[1];

... How to split a string in Java

Community
  • 1
  • 1
FrsECM
  • 245
  • 2
  • 16
  • Indeed this is the best solution, I don't know why they wanted REGEX, it's not the optimal solution in this case. – Mike Jan 27 '14 at 20:44