1

Let's say I have an email address. Like: kate.daniels@somecompany.com.
And I need to break it apart without using split or loops. And that I need to use IndexOf in combination with substring and not using a loop (this will come later after I have a strong understanding of this first).

But looking at this email address with the understanding the email addresses are formatted like: (firstName + "." + lastName + "@" + companyName + ".com")

emailAddress = input.nextLine();
//  This is where I have issues.
firstName = emailAddress.substring(emailAddress.IndexOf(".") );

// How do I get this to be just the last name and not lastName + "@somecompany.com"
lastName = emailAddress.substring(emailAddress.IndexOf(".") + 1);

// And how do I get the company name without the ".com"
companyName = emailAddress.substring(emailAddress.IndexOf("@") + 1);

Thank you for any help you can provide. I can see how this would be very helpful and would like to learn.

I have looked through and I have not seen a response to my question.

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
Martha
  • 13
  • 4

3 Answers3

0

If you find the @ first and divide the string there, you can find the . in either part w/o worrying about the other.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Here is how you would do it in this scenario:

String emailAddress = "kate.daniels@somecompany.com";
String firstName = emailAddress.substring(0, emailAddress.indexOf('.'));
String lastName = emailAddress.substring(emailAddress.indexOf('.') + 1, emailAddress.indexOf('@'));
String companyName = emailAddress.substring(emailAddress.indexOf('@') + 1, emailAddress.lastIndexOf('.'));
System.out.println("Firstname: " + firstName + "\nLastname: " + lastName + "\nCompany: " + companyName); //Print them

Output:

Firstname: kate
Lastname: daniels
Company: somecompany

Substring needs two parameters to get a range, start and end index. Substring with only a single parameter assumes the parameter is the beginning index and returns the rest of the String starting from that location. See the documentation here.

Note that the beginning index for substring is inclusive so for lastName and companyName I needed to put +1 to exclude the . and the @ from the result.

Another important thing to note is for the emailAddress I had to use lastIndexOf(".") because there are two periods in the String and this will ensure you get the final one to substring the correct ranges. Normally indexOf returns the first occurrence.

Nexevis
  • 4,647
  • 3
  • 13
  • 22
  • 1
    Thank you so much! This is exactly what I was looking for! This is really cool. I am new to Java, I come from the database side of the world and to be able to break things up like this is wonderful. – Martha Jul 30 '19 at 19:52
  • @Martha No problem. I know you said not to use split for the problem too, but in the future it might be useful to use something like `String [] s = emailAddress.split("[.@]");` which is much simpler and will put in the 3 names you need in the first 3 array locations! Good luck! – Nexevis Jul 30 '19 at 20:01
0

I believe, that the best solution is to use RegExp. This is much more clear than str.substring().

String email = "kate.daniels@somecompany.com";
Pattern pattern = Pattern.compile("(?<firstName>\w+)\.(?<lastName>\w+)@(?<companyName>\w+)\.\w+");
Matcher matcher = pattern.matcher(email);

if (matcher.matches()) {
    String firstName = matcher.group("firstName");      // kate
    String lastName = matcher.group("lastName");        // daniels
    String companyName = matcher.group("companyName");  // somecompany
}

In case you really want to use str.substring(), then all you need is just find a limit points and use it:

String email = "kate.daniels@somecompany.com";
int dotOne = email.indexOf('.');
int at = email.indexOf('@', dotOne + 1);
int dotTwo = email.indexOf('.', at + 1);

String firstName = email.substring(0, dotOne);        // kate
String lastName = email.substring(dotOne + 1, at);    // daniels
String companyName = email.substring(at + 1, dotTwo); // somecompany
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Thank you for your help on this. I have not yet started using RegExp, but once I do, i will come back to this. Thank you – Martha Jul 30 '19 at 19:54