Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么substring()函数应该返回溢出消息时却返回值_Java_String - Fatal编程技术网

Java 为什么substring()函数应该返回溢出消息时却返回值

Java 为什么substring()函数应该返回溢出消息时却返回值,java,string,Java,String,我试图解决一个问题,它是这样的:- 给定一个字符串,返回一个“向左旋转2”版本,其中前2个字符移动到末尾。字符串长度至少为2 left2("Hello") → "lloHe" left2("java") → "vaja" left2("Hi") → "Hi" 我为此编写了两个函数:- public String left2(String str) { String str1 = str; if(str.length()>2)

我试图解决一个问题,它是这样的:-

给定一个字符串,返回一个“向左旋转2”版本,其中前2个字符移动到末尾。字符串长度至少为2

left2("Hello") → "lloHe"
left2("java") → "vaja"
left2("Hi") → "Hi"
我为此编写了两个函数:-

    public String left2(String str)
    {
         String str1 = str;
          if(str.length()>2)
           str1 = str.substring(2)+str.substring(0,2);
         return str1;
    }

    public String left2(String str)
    {
         return str.substring(2)+str.substring(0,2);
    }

这两种功能都是正确的。我想知道,如果
substring()
函数的第一个参数是索引,那么第二个函数中是否没有
溢出
错误?我这样问是因为Java不是以空字符结尾的,所以我认为第二个函数中有错误。

这是因为只有当文本长度大于2时,子字符串逻辑才起作用。请看这里:

    if(str.length()>2)
       str1 = str.substring(2)+str.substring(0,2);
这是java中
substring
方法的源代码:

public String substring(int beginIndex, int endIndex) {
     if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
     }
     if (endIndex > count) {
          throw new StringIndexOutOfBoundsException(endIndex);
     }
     if (beginIndex > endIndex) {
           throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
     }
     return ((beginIndex == 0) && (endIndex == count)) ? this :
           new String(offset + beginIndex, endIndex - beginIndex, value);
}
公共字符串子字符串(int-beginIndex,int-endIndex){
如果(beginIndex<0){
抛出新的StringIndexOutOfBoundsException(beginIndex);
}
如果(结束索引>计数){
抛出新的StringIndexOutOfBoundsException(endIndex);
}
如果(beginIndex>endIndex){
抛出新的StringIndexOutOfBoundsException(endIndex-beginIndex);
}
返回((beginIndex==0)和(&(endIndex==count))?这是:
新字符串(偏移量+开始索引,结束索引-开始索引,值);
}
根据经验,文档可能是谎言,但代码不是<代码>StringIndexOutOfBoundsException仅在以下情况下才会引发

  • a。beginIndex小于0(即为负)或
  • b。endIndex大于count或
  • c。beginIndex大于endIndex

    • 这是因为只有当文本长度大于2时,子字符串逻辑才起作用。请看这里:

          if(str.length()>2)
             str1 = str.substring(2)+str.substring(0,2);
      
      这是java中
      substring
      方法的源代码:

      public String substring(int beginIndex, int endIndex) {
           if (beginIndex < 0) {
                  throw new StringIndexOutOfBoundsException(beginIndex);
           }
           if (endIndex > count) {
                throw new StringIndexOutOfBoundsException(endIndex);
           }
           if (beginIndex > endIndex) {
                 throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
           }
           return ((beginIndex == 0) && (endIndex == count)) ? this :
                 new String(offset + beginIndex, endIndex - beginIndex, value);
      }
      
      公共字符串子字符串(int-beginIndex,int-endIndex){
      如果(beginIndex<0){
      抛出新的StringIndexOutOfBoundsException(beginIndex);
      }
      如果(结束索引>计数){
      抛出新的StringIndexOutOfBoundsException(endIndex);
      }
      如果(beginIndex>endIndex){
      抛出新的StringIndexOutOfBoundsException(endIndex-beginIndex);
      }
      返回((beginIndex==0)和(&(endIndex==count))?这是:
      新字符串(偏移量+开始索引,结束索引-开始索引,值);
      }
      
      根据经验,文档可能是谎言,但代码不是<代码>StringIndexOutOfBoundsException仅在以下情况下才会引发

      • a。beginIndex小于0(即为负)或
      • b。endIndex大于count或
      • c。beginIndex大于endIndex

        • 这是因为只有当文本长度大于2时,子字符串逻辑才起作用。请看这里:

              if(str.length()>2)
                 str1 = str.substring(2)+str.substring(0,2);
          
          这是java中
          substring
          方法的源代码:

          public String substring(int beginIndex, int endIndex) {
               if (beginIndex < 0) {
                      throw new StringIndexOutOfBoundsException(beginIndex);
               }
               if (endIndex > count) {
                    throw new StringIndexOutOfBoundsException(endIndex);
               }
               if (beginIndex > endIndex) {
                     throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
               }
               return ((beginIndex == 0) && (endIndex == count)) ? this :
                     new String(offset + beginIndex, endIndex - beginIndex, value);
          }
          
          公共字符串子字符串(int-beginIndex,int-endIndex){
          如果(beginIndex<0){
          抛出新的StringIndexOutOfBoundsException(beginIndex);
          }
          如果(结束索引>计数){
          抛出新的StringIndexOutOfBoundsException(endIndex);
          }
          如果(beginIndex>endIndex){
          抛出新的StringIndexOutOfBoundsException(endIndex-beginIndex);
          }
          返回((beginIndex==0)和(&(endIndex==count))?这是:
          新字符串(偏移量+开始索引,结束索引-开始索引,值);
          }
          
          根据经验,文档可能是谎言,但代码不是<代码>StringIndexOutOfBoundsException仅在以下情况下才会引发

          • a。beginIndex小于0(即为负)或
          • b。endIndex大于count或
          • c。beginIndex大于endIndex

            • 这是因为只有当文本长度大于2时,子字符串逻辑才起作用。请看这里:

                  if(str.length()>2)
                     str1 = str.substring(2)+str.substring(0,2);
              
              这是java中
              substring
              方法的源代码:

              public String substring(int beginIndex, int endIndex) {
                   if (beginIndex < 0) {
                          throw new StringIndexOutOfBoundsException(beginIndex);
                   }
                   if (endIndex > count) {
                        throw new StringIndexOutOfBoundsException(endIndex);
                   }
                   if (beginIndex > endIndex) {
                         throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
                   }
                   return ((beginIndex == 0) && (endIndex == count)) ? this :
                         new String(offset + beginIndex, endIndex - beginIndex, value);
              }
              
              公共字符串子字符串(int-beginIndex,int-endIndex){
              如果(beginIndex<0){
              抛出新的StringIndexOutOfBoundsException(beginIndex);
              }
              如果(结束索引>计数){
              抛出新的StringIndexOutOfBoundsException(endIndex);
              }
              如果(beginIndex>endIndex){
              抛出新的StringIndexOutOfBoundsException(endIndex-beginIndex);
              }
              返回((beginIndex==0)和(&(endIndex==count))?这是:
              新字符串(偏移量+开始索引,结束索引-开始索引,值);
              }
              
              根据经验,文档可能是谎言,但代码不是<代码>StringIndexOutOfBoundsException仅在以下情况下才会引发

              • a。beginIndex小于0(即为负)或
              • b。endIndex大于count或
              • c。beginIndex大于endIndex

              第二个函数的原因

              return str.substring(2)+str.substring(0,2);
              
              工作原理是您从未尝试传递短于两个字符的字符串。如果您这样做了,您将收到一个异常()


              当您传递一个两个字符的字符串时,这种方法起作用的原因是允许您传递的索引等于字符串的长度(在本例中为
              2
              )。当您将等于字符串长度的索引传递给包含一个参数的
              子字符串
              时,将得到一个空字符串。当您将一个等于字符串长度的索引传递给两个参数
              子字符串时,它会将字符串带到并包括最后一个字符。

              第二个函数的原因

              return str.substring(2)+str.substring(0,2);
              
              工作原理是您从未尝试传递短于两个字符的字符串。如果您这样做了,您将收到一个异常()

              当您传递一个两个字符的字符串时,这种方法起作用的原因是允许您传递的索引等于字符串的长度(在本例中为
              2
              )。当您将等于字符串长度的索引传递给包含一个参数的
              子字符串
              时,将得到一个空字符串。当您将一个等于字符串长度的索引传递给两个参数
              子字符串时,它会将字符串带到并包括最后一个