src/share/classes/java/lang/String.java

Print this page




1654      */
1655     private int lastIndexOfSupplementary(int ch, int fromIndex) {
1656         if (Character.isValidCodePoint(ch)) {
1657             final char[] value = this.value;
1658             char hi = Character.highSurrogate(ch);
1659             char lo = Character.lowSurrogate(ch);
1660             int i = Math.min(fromIndex, value.length - 2);
1661             for (; i >= 0; i--) {
1662                 if (value[i] == hi && value[i + 1] == lo) {
1663                     return i;
1664                 }
1665             }
1666         }
1667         return -1;
1668     }
1669 
1670     /**
1671      * Returns the index within this string of the first occurrence of the
1672      * specified substring.
1673      *
1674      * <p>The returned index is the smallest value <i>k</i> for which:
1675      * <blockquote><pre>
1676      * this.startsWith(str, <i>k</i>)
1677      * </pre></blockquote>
1678      * If no such value of <i>k</i> exists, then {@code -1} is returned.
1679      *
1680      * @param   str   the substring to search for.
1681      * @return  the index of the first occurrence of the specified substring,
1682      *          or {@code -1} if there is no such occurrence.
1683      */
1684     public int indexOf(String str) {
1685         return indexOf(str, 0);
1686     }
1687 
1688     /**
1689      * Returns the index within this string of the first occurrence of the
1690      * specified substring, starting at the specified index.
1691      *
1692      * <p>The returned index is the smallest value <i>k</i> for which:
1693      * <blockquote><pre>
1694      * <i>k</i> &gt;= fromIndex {@code &&} this.startsWith(str, <i>k</i>)
1695      * </pre></blockquote>
1696      * If no such value of <i>k</i> exists, then {@code -1} is returned.

1697      *
1698      * @param   str         the substring to search for.
1699      * @param   fromIndex   the index from which to start the search.
1700      * @return  the index of the first occurrence of the specified substring,
1701      *          starting at the specified index,
1702      *          or {@code -1} if there is no such occurrence.
1703      */
1704     public int indexOf(String str, int fromIndex) {
1705         return indexOf(value, 0, value.length,
1706                 str.value, 0, str.value.length, fromIndex);
1707     }
1708 
1709     /**
1710      * Code shared by String and AbstractStringBuilder to do searches. The
1711      * source is the character array being searched, and the target
1712      * is the string being searched for.
1713      *
1714      * @param   source       the characters being searched.
1715      * @param   sourceOffset offset of the source string.
1716      * @param   sourceCount  count of the source string.


1763             if (i <= max) {
1764                 int j = i + 1;
1765                 int end = j + targetCount - 1;
1766                 for (int k = targetOffset + 1; j < end && source[j]
1767                         == target[k]; j++, k++);
1768 
1769                 if (j == end) {
1770                     /* Found whole string. */
1771                     return i - sourceOffset;
1772                 }
1773             }
1774         }
1775         return -1;
1776     }
1777 
1778     /**
1779      * Returns the index within this string of the last occurrence of the
1780      * specified substring.  The last occurrence of the empty string ""
1781      * is considered to occur at the index value {@code this.length()}.
1782      *
1783      * <p>The returned index is the largest value <i>k</i> for which:
1784      * <blockquote><pre>
1785      * this.startsWith(str, <i>k</i>)
1786      * </pre></blockquote>
1787      * If no such value of <i>k</i> exists, then {@code -1} is returned.
1788      *
1789      * @param   str   the substring to search for.
1790      * @return  the index of the last occurrence of the specified substring,
1791      *          or {@code -1} if there is no such occurrence.
1792      */
1793     public int lastIndexOf(String str) {
1794         return lastIndexOf(str, value.length);
1795     }
1796 
1797     /**
1798      * Returns the index within this string of the last occurrence of the
1799      * specified substring, searching backward starting at the specified index.
1800      *
1801      * <p>The returned index is the largest value <i>k</i> for which:
1802      * <blockquote><pre>
1803      * <i>k</i> {@code <=} fromIndex {@code &&} this.startsWith(str, <i>k</i>)
1804      * </pre></blockquote>
1805      * If no such value of <i>k</i> exists, then {@code -1} is returned.

1806      *
1807      * @param   str         the substring to search for.
1808      * @param   fromIndex   the index to start the search from.
1809      * @return  the index of the last occurrence of the specified substring,
1810      *          searching backward from the specified index,
1811      *          or {@code -1} if there is no such occurrence.
1812      */
1813     public int lastIndexOf(String str, int fromIndex) {
1814         return lastIndexOf(value, 0, value.length,
1815                 str.value, 0, str.value.length, fromIndex);
1816     }
1817 
1818     /**
1819      * Code shared by String and AbstractStringBuilder to do searches. The
1820      * source is the character array being searched, and the target
1821      * is the string being searched for.
1822      *
1823      * @param   source       the characters being searched.
1824      * @param   sourceOffset offset of the source string.
1825      * @param   sourceCount  count of the source string.




1654      */
1655     private int lastIndexOfSupplementary(int ch, int fromIndex) {
1656         if (Character.isValidCodePoint(ch)) {
1657             final char[] value = this.value;
1658             char hi = Character.highSurrogate(ch);
1659             char lo = Character.lowSurrogate(ch);
1660             int i = Math.min(fromIndex, value.length - 2);
1661             for (; i >= 0; i--) {
1662                 if (value[i] == hi && value[i + 1] == lo) {
1663                     return i;
1664                 }
1665             }
1666         }
1667         return -1;
1668     }
1669 
1670     /**
1671      * Returns the index within this string of the first occurrence of the
1672      * specified substring.
1673      *
1674      * <p>The returned index is the smallest value {@code k} for which:
1675      * <pre>{@code
1676      * this.startsWith(str, k)
1677      * }</pre>
1678      * If no such value of {@code k} exists, then {@code -1} is returned.
1679      *
1680      * @param   str   the substring to search for.
1681      * @return  the index of the first occurrence of the specified substring,
1682      *          or {@code -1} if there is no such occurrence.
1683      */
1684     public int indexOf(String str) {
1685         return indexOf(str, 0);
1686     }
1687 
1688     /**
1689      * Returns the index within this string of the first occurrence of the
1690      * specified substring, starting at the specified index.
1691      *
1692      * <p>The returned index is the smallest value {@code k} for which:
1693      * <pre>{@code
1694      *     k >= Math.min(fromIndex, this.length()) &&
1695      *                   this.startsWith(str, k)
1696      * }</pre>
1697      * If no such value of {@code k} exists, then {@code -1} is returned.
1698      *
1699      * @param   str         the substring to search for.
1700      * @param   fromIndex   the index from which to start the search.
1701      * @return  the index of the first occurrence of the specified substring,
1702      *          starting at the specified index,
1703      *          or {@code -1} if there is no such occurrence.
1704      */
1705     public int indexOf(String str, int fromIndex) {
1706         return indexOf(value, 0, value.length,
1707                 str.value, 0, str.value.length, fromIndex);
1708     }
1709 
1710     /**
1711      * Code shared by String and AbstractStringBuilder to do searches. The
1712      * source is the character array being searched, and the target
1713      * is the string being searched for.
1714      *
1715      * @param   source       the characters being searched.
1716      * @param   sourceOffset offset of the source string.
1717      * @param   sourceCount  count of the source string.


1764             if (i <= max) {
1765                 int j = i + 1;
1766                 int end = j + targetCount - 1;
1767                 for (int k = targetOffset + 1; j < end && source[j]
1768                         == target[k]; j++, k++);
1769 
1770                 if (j == end) {
1771                     /* Found whole string. */
1772                     return i - sourceOffset;
1773                 }
1774             }
1775         }
1776         return -1;
1777     }
1778 
1779     /**
1780      * Returns the index within this string of the last occurrence of the
1781      * specified substring.  The last occurrence of the empty string ""
1782      * is considered to occur at the index value {@code this.length()}.
1783      *
1784      * <p>The returned index is the largest value {@code k} for which:
1785      * <pre>{@code
1786      * this.startsWith(str, k)
1787      * }</pre>
1788      * If no such value of {@code k} exists, then {@code -1} is returned.
1789      *
1790      * @param   str   the substring to search for.
1791      * @return  the index of the last occurrence of the specified substring,
1792      *          or {@code -1} if there is no such occurrence.
1793      */
1794     public int lastIndexOf(String str) {
1795         return lastIndexOf(str, value.length);
1796     }
1797 
1798     /**
1799      * Returns the index within this string of the last occurrence of the
1800      * specified substring, searching backward starting at the specified index.
1801      *
1802      * <p>The returned index is the largest value {@code k} for which:
1803      * <pre>{@code
1804      *     k <= Math.min(fromIndex, this.length()) &&
1805      *                   this.startsWith(str, k)
1806      * }</pre>
1807      * If no such value of {@code k} exists, then {@code -1} is returned.
1808      *
1809      * @param   str         the substring to search for.
1810      * @param   fromIndex   the index to start the search from.
1811      * @return  the index of the last occurrence of the specified substring,
1812      *          searching backward from the specified index,
1813      *          or {@code -1} if there is no such occurrence.
1814      */
1815     public int lastIndexOf(String str, int fromIndex) {
1816         return lastIndexOf(value, 0, value.length,
1817                 str.value, 0, str.value.length, fromIndex);
1818     }
1819 
1820     /**
1821      * Code shared by String and AbstractStringBuilder to do searches. The
1822      * source is the character array being searched, and the target
1823      * is the string being searched for.
1824      *
1825      * @param   source       the characters being searched.
1826      * @param   sourceOffset offset of the source string.
1827      * @param   sourceCount  count of the source string.