简体   繁体   English

使用IndexOf()查找字符串中字符的第n次出现

[英]Finding the nth occurrence of a character in a String using IndexOf()

I have a question regarding indexOf() . 我有一个关于indexOf()的问题。 I am trying to program an EmailExtractor (Yes, this is a homework but I am not looking for code) which extracts the entire email address from a sentence that is input by a user. 我正在尝试对EmailExtractor进行编程(是的,这是一项家庭作业,但我不是在寻找代码),该程序将从用户输入的句子中提取整个电子邮件地址。

For example - User Input: Mail us at abc@def.ghi.jk with your queries. 例如- 用户输入:将您的查询通过abc@def.ghi.jk发送给我们。 The program will then display abc@def.ghi.jk from the above String . 然后,程序将显示上述String abc@def.ghi.jk I understand indexOf() and substring() are required. 我了解indexOf()substring()是必需的。

The idea I have now is to use indexOf() to locate the '@', and then search for the empty space just before the email address input by the user (nth). 我现在的想法是使用indexOf()定位“ @”,然后在用户输入的电子邮件地址(第n个)之前搜索空白。

My code is as follows: 我的代码如下:

System.out.println("This is an Email Address Extractor.\n");
System.out.print("Enter a line of text with email address: ");
String emailInput = scn.nextLine();
int spaceAt = emailInput.indexOf(" ");
for (int i = 1; i <= emailInput.indexOf("@"); i++){
    if (spaceAt < emailInput.indexOf("@")) {
        spaceAt = emailInput.indexOf(" ", spaceAt + 1);
    }
}

I understand and am aware of the problem in my code. 我理解并意识到我代码中的问题。

1) "Mail us at abc@def.ghi.jk with your queries".indexOf(" ") is 4, I am trying to get 10. However, the IF Condition I have input will cause it to skip to the next instance of indexOf() which is 25. (Because 10 < 14). 1) "Mail us at abc@def.ghi.jk with your queries".indexOf(" ")为4,我试图获取10。但是,如果我输入的IF条件会使它跳到下一个实例indexOf()的值为25。(因为10 <14)。

How do I go about avoiding this from happening? 我如何避免这种情况的发生?

Once again, I am not looking for purely the answer rather, I am trying to work around a solution. 再一次,我不是纯粹在寻找答案,而是在尝试解决方案。 Thanks in advance! 提前致谢!

How about finding the space before and after the @ 如何在@前后查找空间

int at = emailInput.indexOf('@');
int start = emailInput.lastIndexOf(' ', at) + 1;
int end = emailInput.indexOf(' ', at);
if (end == -1) end = emailInput.length();
String email = emailInput.substring(start, end);

you can use regular expressions instead of using indexOf() and substring() 您可以使用正则表达式而不是使用indexOf()substring()

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"

use the Pattern and Matcher to validate the email id in your string. 使用模式和匹配器来验证字符串中的电子邮件ID。 This may give you a clear view on it 可以使您清楚地了解它

One thing that you could do is call indexOf and test the result in the same iteration of the loop, instead of saving the check for the next iteration. 您可以做的一件事是调用indexOf并在循环的同一迭代中测试结果,而不是保存下一次迭代的检查。 Something like this in mostly pseudocode: 类似这样的东西大多是伪代码:

spaceAt := 0
indexOfAtSign := emailInput.indexOf('@');
while (spaceAt < indexOfAtSign) { // you never really used i anyway
    temp := index of next space
    if (temp > indexOfAtSign) break;
    else spaceAt = temp;
}

(There'd need to be a small bit of corner-case testing, but only for if there are no spaces either before the @ sign or after the @ sign) (只需要进行少量的极端情况测试,仅适用于@符号之前或@符号之后没有空格的情况)

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

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