1

I am using jsp for a web based project. i want to remove all the characters after '@' including '@'.

suppose I got "stackoverflow@stack.com" as input from a user. I have to remove the characters after @ and also '@' i.e.

o/p is stackoverflow

I think one way is using java array OR by regex. Please suggest me the better way of solving this problem. Are there any other ways of solving this??

How can I do it using JSP? I can not use javascript for this only jsp.

Thank You.

4

1 回答 1

0

为什么不使用 Java 字符串?

单程:

String output = input;
int at = input.indexOf('@');
if (at != -1) {
    output = input.substring(0, at); 
}

(它可以优化为更少的行,但这是最清晰的编写方式)。

或者

String output = input.split("@", 2)[0];
// I prefer the first way, this is a bit more wasteful, but that is probably
// just a personal hang up from the days of embedded C ;-)
于 2012-08-01T04:24:10.093 回答