0

I have a following line : |A|B|C|D|100|E|GEN|smsplus|11|11|11| and I want to remove the first and last occurrence of |.

I have to check first if the | is available at first and last

I am sure that this will bring me some down votes but really don't know how to go about.

EDIT

I also have to check for the following :

A|B|C|D|100|E|GEN|smsplus|11|11|11| and |A|B|C|D|100|E|GEN|smsplus|11|11|11 and there is no 

possibilities for || that is why I did not want to give any explanation for the same.

thanks for your valuable time.

Java Questions
  • 7,813
  • 41
  • 118
  • 176

4 Answers4

6

You can perhaps use the regex:

^\\|(.*)\\|$

And replace by $1:

ideone demo

If the start and end of the string are not both |, there'll be no replacement.

EDIT: As per your last comment, if you want to remove the | if it's either at the start and/or at the end of the string, then use an OR operator (there's too many | in that question I think =P):

^\\||\\|$

Replace by nothing (empty string "")


EDIT2:

If the first regex is closer to what you need but also want to remove multiple pipes, add quantifiers:

^\\|+(.*)\\|+$

If the second regex (in first edit) is what you need but also want to remove multiple pipes, add quantifiers again!

^\\|+|\\|+$

Note that the replacement strings remain the same in both cases

Jerry
  • 70,495
  • 13
  • 100
  • 144
  • 1
    According to the comments, it's possible that only one side of the string is a `|`, and that should be removed. – Tim Pietzcker Sep 30 '13 at 07:49
  • @TimPietzcker Yes, I saw it just a while ago... The question wasn't well formulated after all :( – Jerry Sep 30 '13 at 07:51
  • According to his comments "if | is not available at first and last than what will happen if I use your code?" and his question "I have to check first if the | is available at first and last" I think OP wants to remove the characters if they are present both at the beginning and at the end. – justhalf Sep 30 '13 at 07:51
  • @justhalf But in another one of his comments, OP has: "All, I want to remove | if it is the first one and **if** available in the last one also` And I only added one option, depending on what he's really looking for. He can pick the one he needs. – Jerry Sep 30 '13 at 07:55
  • Yes, the question is quite unclear. How if there are multiple pipes at the end like `|AB|CD|EF||`? – justhalf Sep 30 '13 at 07:57
  • @justhalf I can add yet another option for him to choose =P – Jerry Sep 30 '13 at 07:58
  • Haha, I hope he wants to convert `|AB|CD|EF||` into `AB|CD|EF`, for which `StringUtils.strip(input,"|")` would be very elegant, and that this question would be indeed a duplicate for http://stackoverflow.com/questions/2088037/trim-characters-in-java haha – justhalf Sep 30 '13 at 08:00
  • @Jerry the code is working if the `|` exist `first and last` and the code is not working for `A|B|C|D|100|E|GEN|smsplus|11|11|11|` and `|A|B|C|D|100|E|GEN|smsplus|11|11|11` – Java Questions Sep 30 '13 at 08:01
  • @justhalf Might be the case. Sometimes, we our mindreading powers aren't far reaching. – Jerry Sep 30 '13 at 08:02
  • @Anto Use the second regex I mentioned in my answer. Or use TimPietzcker's answer. – Jerry Sep 30 '13 at 08:13
4

Your comments lead me to believe that you want to do this:

String result = subject.replaceAll("^\\||\\|$", "");

This will change each of the following strings:

|A|B|C|
|A|B|C
A|B|C|
A|B|C

to the same string A|B|C.

See it on regex101.com.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
3

Use indexOf() and lastIndexOf() from java.lang.String package

    String s="|A|B|C|D|100|E|GEN|smsplus|11|11|11|";
    s=s.substring(s.indexOf("|"),s.lastIndexOf("|"));
    System.out.println(s);    //Prints A|B|C|D|100|E|GEN|smsplus|11|11|11
kark
  • 4,763
  • 6
  • 30
  • 44
2

Try,

String input = "|A|B|C|D|100|E|GEN|smsplus|11|11|11|";
input = input.substring(1, input.length()-1);

or use StringUtils.removeStart, removeEnd

StringUtils.removeStart(input, "|");
StringUtils.removeEnd(input, "|");

or

StringUtils.strip(input, "|");
newuser
  • 8,338
  • 2
  • 25
  • 33