简体   繁体   English

使用IndexOf和子字符串将字符串分开,而不使用拆分或循环

[英]Breaking apart a String using IndexOf and substring without using split or a loop

Let's say I have an email address. 假设我有一个电子邮件地址。 Like: kate.daniels@somecompany.com. 像: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). 而且我需要结合使用IndexOf和子字符串,而不要使用循环(这将在我对此有很深的理解之后再出现)。

But looking at this email address with the understanding the email addresses are formatted like: (firstName + "." + lastName + "@" + companyName + ".com") 但是在了解此电子邮件地址的前提下,电子邮件地址的格式如下: (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. 我浏览了一下,但没有看到对我的问题的答复。

If you find the @ first and divide the string there, you can find the . 如果先找到@然后在其中分割字符串,则可以找到. in either part w/o worrying about the other. 在任何一个方面都不会担心另一个。

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需要两个参数才能获得range ,开始索引和结束索引。 Substring with only a single parameter assumes the parameter is the beginning index and returns the rest of the String starting from that location. 仅具有单个参数的Substring假定该参数是开始索引,并从该位置开始返回String的其余部分。 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 . 请注意, substring的开始索引包含所有内容,因此对于lastNamecompanyName我需要将+1排除在外. 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. 另一个要注意的重要事项是我必须使用lastIndexOf(".")emailAddress因为String有两个句点,这将确保您得到最后一个将正确范围的substring Normally indexOf returns the first occurrence. 通常, indexOf返回第一个匹配项。

I believe, that the best solution is to use RegExp . 我相信,最好的解决方案是使用RegExp This is much more clear than str.substring() . 这比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: 如果您真的想使用str.substring() ,那么您只需要找到一个极限点并使用它即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM