【问题标题】:how to remove characters of String from left in java [closed]如何在java中从左侧删除字符串的字符[关闭]
【发布时间】:2013-12-03 07:52:02
【问题描述】:

我在 java 中有一个字符串,其中包含以下字符..

'2010-12-04' and '2013-12-03' and wwid='1234'

现在根据我的需要,我必须删除它的起始字符并使其像..

and wwid='1234'

我通过子字符串概念尝试了它,我尝试给出需要删除的起点,然后将其添加到字符串中,但我无法得到它..

请帮我解决这个问题。 提前致谢

【问题讨论】:

    标签: java string substring


    【解决方案1】:

    你可以先找到最后一个Indexof Stringand,并把它作为开始位置做substring方法。

    试试

       String text = "'2010-12-04' and '2013-12-03' and wwid='1234'";
       text = text.substring(text.lastIndexOf("and"));
       System.out.println(text);
    

    控制台输出:

    and wwid='1234'
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        实际上子字符串应该可以工作。您需要考虑的一件事是 substring 不会更改实际字符串,而是返回一个新字符串。所以使用:

        String newString = str.substring(30);
        

        【讨论】:

          【解决方案4】:

          你可以试试这个:

          String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
          String myNewString = input.substring(numberOfPositionsToRemove);
          

          在你的情况下是:

          String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
          String myNewString = input.substring(30);
          

          或者为了让它更有活力,你可以使用:

          String input = "'2010-12-04' and '2013-12-03' and wwid='1234'"
          int index = input.lastIndexOf("and");
          String myNewString = input.substring(index);
          

          【讨论】: