【问题标题】:How to display the characters upto a specific index of a String using String function?如何使用字符串函数将字符显示到字符串的特定索引?
【发布时间】:2016-07-08 08:52:44
【问题描述】:

我的字符串定义为

text1:text2:text3:text4:text5

我想得到输出

文本1:文本2:文本3

使用字符串方法。

我尝试过使用lastIndexOf,然后是substring,然后又是lastIndexOf。 我想通过两次调用lastIndexOf 来避免这三个步骤。
有没有更好的方法来实现这一点?

【问题讨论】:

  • 您能进一步解释您的问题吗?目前尚不清楚您要解决的问题是什么,因为您给出的示例过于抽象且不够具体,以至于人们无法提供良好的答案。

标签: java string


【解决方案1】:

您可以通过运行一个循环来遍历从 index = 0 到 index = lastIndexOf('3') 的字符串字符来做到这一点。代码如下:

String s = "text1:text2:text3:text4:text5";
for(int i = 0; i < = s.lastIndexOf('3'); i++)
    System.out.print(s.charAt(i));

这将为您提供所需的输出。

输出

text1:text2:text3

【讨论】:

    【解决方案2】:

    可以使用正则表达式来识别字符串的正确部分:

    private static Pattern PATTERN = Pattern.compile("([^:]*:){2}[^:]*(?=:|$)");
    
    public static String find(String input) {
        Matcher m = PATTERN.matcher(input);
        return m.find() ? m.group() : null;
    }
    

    或者在每次调用lastIndexOf 之间不要使用substring,而是使用限制索引范围的lastIndexOf 版本:

    public static String find(String input, int colonCount) {
        int lastIndex = input.length();
        while (colonCount > 0) {
            lastIndex = input.lastIndexOf(':', lastIndex-1);
            colonCount--;
        }
        return lastIndex >= 0 ? input.substring(0, lastIndex) : null;
    }
    

    请注意,这里的colonCount 是字符串中遗漏的: 的数量。

    【讨论】:

      【解决方案3】:

      你可以试试:

      String test = "text1:text2:text3:text4:text5";
      String splitted = text.split(":")
      String result = "";
      for (int i = 0; i <3; i++) {
          result += splitted[i] + ":"
      }
      result = result.substring(0, result.length() -1)
      

      【讨论】:

        【解决方案4】:

        你可以使用Javasplit()-方法:

        String string = "text1:text2:text3:text4:text5";
        String[] text = string.split(":");
        String text1 = text[0]; 
        String text2 = text[1];
        String text3 = text[2];
        

        然后直接或者用for循环生成输出:

        // directly
        System.out.println(text1 + ":" + text2 + ":" + text3);
        
        // for-loop. Just enter, how many elements you want to display. 
        for(int i = 0; i < 3; i++){
            System.out.println(text[i] + " ");
        }
        

        输出:

        text1 text2 text3
        

        使用这种方法的好处是,您的输入和输出可能会稍微复杂一些,因为您可以控制单词的打印顺序。

        示例:

        想想尤达大师。

        他说话的方式很奇怪,经常混淆句子结构。当他介绍自己时,他说了一句(不正确的!)句子:“尤达大师,我的名字是”。

        现在,您想创建一个通用翻译器,当然,它可以在从一个物种翻译到另一个物种时修正这些错误。

        您接收输入字符串并将其“划分”为各个部分:

        String string = "Master:Yoda:my:name:is"
        String[] text = string.split(":");
        String jediTitle = text[0];
        String lastName = text[1]; 
        String posessivePronoun = text[2];
        String noun = text[3];
        String linkingVerb = text[4];
        

        数组“text”现在按照您输入的顺序包含句子。现在您的翻译人员可以分析结构并更正它:

        String correctSentenceStructure = posessivePronoun + " " + noun + " "  + linkingVerb + " "  + jediTitle + " " + lastName;    
        System.out.println(correctSentenceStructure);
        

        输出:

        "My name is Master Yoda"
        

        一名工作翻译可能是迈向银河系的又一步。

        【讨论】:

          【解决方案5】:

          不妨试试这个s.substring(0, s.lastIndexOf('3')+1);

          完整示例:

          package testing.project;
          
          public class Main {
          
              public static void main(String[] args) {
                  String s = "text1:text2:text3:text4:text5";
                  System.out.println(s.substring(0, s.lastIndexOf('3')+1));
              }
          }
          

          输出:

          文本1:文本2:文本3

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-06-29
            • 2017-01-14
            • 2012-01-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多