【问题标题】:Nth occurrence of a character and if conditionals第 N 次出现一个字符和 if 条件
【发布时间】:2017-04-11 10:16:44
【问题描述】:

假设我们有两个字符串(ULR):

https://stackoverflow.com/questions/ask
https://web.whatsapp.com/

我需要写如下表达式:

如果在第三个斜线 (/) 之后什么都没有或第三个斜线不存在,则做

{
some operation
} else {
another action
}

请帮忙。

        List<String> list = new ArrayList<String>();

        while((str = in.readLine()) != null){
            list.add(str);
        }

    String[] stringArr = list.toArray(new String[0]);

    //copying and removing https from the list

     List<String> list2 =  new ArrayList<String>();
          Collections.addAll(list2, stringArr);
          Iterator<String> iter = list2.iterator();
          while(iter.hasNext()){
              if(iter.next().contains(https))
                  // here you should copy https lines to another file. 
                  iter.remove();
          } 

          String[] stringArr2 = list2.toArray(new String[0]);



        for (int i = 0; i<stringArr2.length; i++) {
            //condition for pure domain names.
            //else the action below



            System.out.println(getDomainName(stringArr2[i]) + "," + stringArr2[i] + "," + "U" +"," + number_of_doc + "," + today);
        }




    }

    public static String getDomainName(String url) throws URISyntaxException {
    URI uri = new URI(url);
    String domain = uri.getHost();
    return domain.startsWith("www.") ? domain.substring(4) : domain;
}





}

【问题讨论】:

    标签: java string if-statement


    【解决方案1】:

    为什么不分开:

    String link = "https://stackoverflow.com/questions/ask ";
    if (link.split("/").length >= 3 ) {
        System.out.println("CORRECT");
    }else{
        System.out.println("NOT CORRECT");
    }
    

    想法是:/分割你的字符串,如果结果很好或等于3,那么你的条件是正确的,否则不正确。

    编辑

    或者像评论中提到的@biziclop,您可以使用Guava's Splitter,例如:

    Iterable<String> result = 
            Splitter.on(CharMatcher.anyOf("/")).
                    limit(4).
                    split("https://stackoverflow.com/questions/ask");
    
    if (Lists.newArrayList(result).size() > 3) {
        System.out.println(Lists.newArrayList(result).get(3));
    }else{
        System.out.println("NOTHING");
    }
    

    输入

    https://stackoverflow.com/questions/ask
    https://stackoverflow.com
    

    输出

    questions/ask
    NOTHING
    

    【讨论】:

    • 如果你使用 Guava 的 Splitter,你甚至可以指定你获得的令牌数量的上限,这样你就可以将第三个 / 之后的所有内容作为单个字符串检索。
    • 我知道这是个好主意@biziclop 谢谢,你的意思是这个Splitter.on(CharMatcher.anyOf("/")).omitEmptyStrings().split("https://stackoverflow.com/questions/ask")
    • 差不多,你只需要不调用omitEmptyStrings(),而是调用limit(4)。这样,返回的第 4 个元素将是 questions/ask。如果您获得的元素少于 4 个,则您的字符串中没有 3 个 /s。
    • 谢谢@biziclop 提供的信息,我很感激;)我已经编辑了我的答案,如果我有错误,谢谢你告诉我
    【解决方案2】:

    你可以使用简单的正则表达式:

        String url = "https://web.whatsapp.com/";
    
        if(url.matches("\\w+://(\\w+.\\w+)+(.\\w+)*/(.+)"))
            System.out.println("Correct URL");
        else
            System.out.println("Incorrect URL");
    

    【讨论】:

      猜你喜欢
      • 2015-11-16
      • 2012-04-26
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 2022-01-18
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多