1

I want to extract a string between two characters "@" and the first ".".I just tried the code but my code is not appropriate and getting different output

//code 
    static String method(String s){ 
       String result=s.substring(s.lastIndexOf("@"));
       int index = result.indexOf("."); 
       String finalResult= result.substring(0,index);
    return finalResult;//return 
    }


eg:abc@gmail.com
my output:@gmail
Expected output: gmail
From input gmail is identifies as a string between '@' and the first '.' after it.
But i am getting @ in my output which is different from expected output. Help me out.
anonymous
  • 11
  • 1
  • Use something like this: `String stringWithoutAt = myString.substring(0) if(stringWithoutAt.charAt(0)=="@"){ return stringWithoutAt.substring(1); }`. This code only checks if the first character of the string is "@", and if it is, then returns the string without the first character, "@", so add the other checks. I learnt how to use substring from [this](https://stackoverflow.com/a/67923195/16136190) and get a specific character using `.charAt()` from [this](https://stackoverflow.com/a/8000852/16136190). – The Amateur Coder Jan 27 '22 at 07:25

2 Answers2

2

As per Javadocs:

Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" //(an empty string because there are no more characters after the 9th, i.e., there's no 10th character in "emptiness"; "emptiness" is a 9-letter word)

And if you debug your code, you will see

System.out.println("abc@gmail.com".lastIndexOf("@"));

produces 3

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

As mentioned by Scary Wombat, substring(position) is inclusive, and therefore s.substring(s.lastIndexOf("@")); will return substring containing '@' char.

Some extra note is that there are be many edge cases for which your code will fail, for example null string, string that does not contain '@' etc.

See suggestion code that tries to catch all edge cases, with test

@Test
public void testGetSubString(){
    final String constant = "gmail";

    final String input1 = "test@gmail.com";
    final String input2 = "t.est@gmail.com";
    final String input3 = "test@@gmail.com";
    final String input4ShouldBeNull = "test@gmailcom";
    final String input5ShouldBeNull = "test@.gmailcom";
    final String input6ShouldBeNull = "";
    final String input7ShouldBeNull = null;
    final String input8ShouldBeNull = "gmail.com";

    assertEquals(constant, getSubString(input1));
    assertEquals(constant, getSubString(input2));
    assertEquals(constant, getSubString(input3));
    assertNull(constant, getSubString(input4ShouldBeNull));
    assertNull(constant, getSubString(input5ShouldBeNull));
    assertNull(constant, getSubString(input6ShouldBeNull));
    assertNull(constant, getSubString(input7ShouldBeNull));
    assertNull(constant, getSubString(input8ShouldBeNull));

}

public String getSubString(final String input) {
    if(input == null) {
        return null;
    }

    final int indexOfAt = input.lastIndexOf('@');
    if(input.isEmpty() || indexOfAt < 0 || indexOfAt >= input.length()-2) {
        return null;
    }

    String suffix = input.substring(indexOfAt + 1);

    final int indexOfDot = suffix.indexOf('.');
    if(indexOfDot < 1) {
        return null;
    }

    return suffix.substring(0, indexOfDot);
}
Orr Benyamini
  • 395
  • 2
  • 9