-2

I am trying to replace this string

String sampl =  "Mab by Gap David . Leave my Food"

with this code:

String authorf = sampl.replace(".","").replaceAll("Leave.","");

so that at the end I will have only Mab by Gap David.

But currently I am getting Mab by Gap David my Food

Please how do I get only Mab by Gap David ?

X09
  • 3,827
  • 10
  • 47
  • 92
  • 3
    Well, your code does exactly what you ask it to do. `replace(".","")` removed all dots, and `replaceAll("Leave.","")` removed `Leave` and one character after it (since `replaceAll` is using regex and `.` in regex represents any character except line separators like `\r` `\n`). – Pshemo May 15 '16 at 00:21
  • You want just the first sentence? Please explain what you're expecting your code to do, so that ppl can realize what you're wanting your perfectly working code to produce. – venkatKA May 15 '16 at 00:24
  • @venkatKA, yes I want just the first sentence. – X09 May 15 '16 at 00:27
  • You can use one of standard classes which allows us to iterate over sentences http://stackoverflow.com/a/17983763/1393766 – Pshemo May 15 '16 at 00:28

2 Answers2

3

If you want just the first sentence, that means deleting anything after the sentence terminator.

From your example, it seems you want the sentence terminator itself to be removed too. Removing any spaces before the terminator seems logical as well.

A sentence terminator can be defined as a period (.), a question mark (?), or an exclamation point (!). You can of course decide for yourself what sentence terminators you want.

So. Find a sentence terminator: [.?!]
Also select any spaces before: \s*[.?!]
Also select anything after: \s*[.?!].*

So your code would be:

String authorf = sampl.replaceAll("\\s*[.?!].*","");

See regex101.com for example.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • This is hard to maintain and only works for the specific case. – Lee May 15 '16 at 00:41
  • Better solution. Thanks. Serves my purpose because I want to remove both the full stop and everything after it. – X09 May 15 '16 at 00:42
  • @Lee Which part is hard to maintain? What special case? It's way less specialized than your answer of replacing `. Leave my Food`. Now *that* only works for the specific case. – Andreas May 15 '16 at 00:43
  • In no part part of the OP does it state 'I want to remove everything after the fullstop' which is what your solution does. Your regex is only right by coincidence, whereas my solution highlights the issues with the question. – Lee May 15 '16 at 00:47
  • @Lee If OP know the *exact* text, why the need for replace? Think about it......... It's an **example**. The only reason to use replace, is that the text is not well-known. – Andreas May 15 '16 at 00:51
  • Those are your assumptions. Just because your solution is more complex doesn't make it correct. – Lee May 15 '16 at 00:54
  • 1
    Actually, Andreas is right, the **Leave my Food** is only an example. Because I fetch the text from external source, other text might come after **Mab by Gap David**. So, while Lee's answer replaces the text in question, it wont't replace say **Job a cat** if it comes after **Mab by Gap David**. But Andreas solution made provision for any text that comes after **Mab by Gap David**. No ills given. – X09 May 15 '16 at 01:22
1

sampl.replace(" . Leave my Food", "")

Lee
  • 738
  • 3
  • 13
  • 2
    If you hardcode `Leave my Food`, then you might as well hardcode result: `String authorf = "Mab by Gap David"`. Wouldn't the point be to do it without knowing the text? – Andreas May 15 '16 at 00:36
  • So what would your solution be? – Lee May 15 '16 at 00:38
  • Replace everything, starting with the period. See [my answer](http://stackoverflow.com/a/37233269/5221149). – Andreas May 15 '16 at 00:39
  • Your solution is hard to read and only fits the specific case. In the OP the Strings are hard coded so why not the solution? Making up some random regex doesn't mean it is better. – Lee May 15 '16 at 00:42
  • 1
    It's only hard to read if you don't know regular expressions. Same as Java is hard to read if you don't know programming. And French is hard to read if you don't know the language. – Andreas May 15 '16 at 00:46
  • This whole argument's isn't necessary. While Lee's answer is not wrong at all, Andreas answer is better. – X09 May 15 '16 at 01:16
  • Andreas' answer is not better simply because it uses regex. – Lee May 15 '16 at 01:17
  • "*Andreas' answer is not better simply because it uses regex*" true, it is not better because it uses regex, but it is better because it solves *general* case, not only specific one from OP example. We see what you did in your answer and we appreciate your humor, but if you think that question should be better explained and you want to show it, please do it via comment. Answers are meant for honest attempts to solve OP *real* problem (which also could lead to improvement of question), not to show problems with question. – Pshemo May 15 '16 at 13:50