String indexOf() int int in Java

indexOf(): This method is available in java.lang.String class of Java.

Syntax:

int java.lang.String.indexOf(int ch, int fromIndex)

This method takes two arguments of int as its parameters. This method returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of the first such occurrence is returned.

Parameters: One parameter is required for this method.

ch: a character (Unicode code point).fromIndex the index to start the search from.

Returns: the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1if the character does not occur.

For Example:

String str = "The World is beutiful!"

int ch = 'e'

str.indexOf(ch,fromIndex) = > It returns 14.

Approach

Java

public class IndexOfIntInt {
    public static void main(String[] args) {

        String str = "The World is beutiful!";

        int ch = 'e';
        int fromIndex = 5;
        System.out.println(str.indexOf(ch, fromIndex));
    }
}

Output:

14


Some more methods of String

charAt() It returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

codePointAt()It returns the character (Unicode code point) at the specified index. The index refers to char values(Unicode code units) and ranges from 0 to length() - 1.

codePointBefore()It returns the character (Unicode code point) before the specified index.

codePointCount()It returns the number of Unicode code points in the specified text range of this String.

compareTo()It compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

compareToIgnoreCase()It compares two strings lexicographically, ignoring case differences.

concat()It concatenates the specified string to the end of this string.

contains()It returns true if and only if this string contains the specified sequence of char values.

contentEquals(CharSequence cs): It compares this string to the specified CharSequence.

contentEquals(StringBuffer sb) It compares this string to the specified StringBuffer.

String.copyValueOf(char[] data)It returns the String representation of the char array characters.

String.copyValueOf(char[] data, int offset, int count) It returns the string that contains the character of the specified sub-array.

endsWith()It tests if this string ends with the specified suffix.

equals()It compares this string to the specified object.

equalsIgnoreCase()It Compares this String to another String, ignoring case considerations.

String.format()It returns a formatted string using the specified format string and arguments.

formatted()It formats using this string as the format string, and the supplied arguments.

String.format(Locale l, String format, Object... args): It returns a formatted string using the specified locale, format string, and arguments.

getBytes()It encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

getBytes(Charset charset): It encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. 

getBytes(String charsetName)It encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

hashCode()It returns a hash code for this string.

indent()It adjusts the indentation of each line of this string based on the value of n and normalizes line termination characters.

indexOf(int ch)It returns the index within this string of the first occurrence of the specified character. 

indexOf(int ch, int fromIndex)It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

indexOf(String str)It returns the index within this string of the first occurrence of the specified substring.

indexOf(String str, int fromIndex)It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

intern()It returns a canonical representation for the string object.

isBlank()It returns true if the string is empty or contains only white space code points, otherwise false.

isEmpty()It returns true if, and only if, length() is 0.

String.join(): It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

lastIndexOf(int ch) It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(int ch, int fromIndex)It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(String str)It returns the index within this string of the last occurrence of the specified substring.

lastIndexOf(String str, int fromIndex): It returns the index within this string of the last occurrence of the specified substring.

length()It returns the length of this string.

matches()It tells whether or not this string matches the given regular expression.

offsetByCodePoints():It returns the index within this String that is offset from the given index by codePointOffset code points.

regionMatches(int toffset, String other, int ooffset, int len)It tests if two string regions are equal.

regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)It tests if two string regions are equal. A substring of this String object is compared to a substring of the argument other.

repeat()It returns a string whose value is the concatenation of this string repeated count times.

replaceAll()It replaces each substring of this string that matches the given regular expression with the given replacement.

replace(char oldChar, char newChar) It returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)It replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst()It replaces the first substring of this string that matches the given regular expression with the given replacement.

split(String regex)It splits this string around matches of the given regular expression.

split(String regex, int limit)It splits this string around matches of the given regular expression.

startsWith(String prefix)It tests if this string starts with the specified prefix.

startsWith(String prefix, int offset)It tests if the substring of this string beginning at the specified index starts with the specified prefix.

strip()It returns a string whose value is this string, with all leading and trailing white space removed.

stripIndent()It returns a string whose value is this string, with incidental white space removed from the beginning and end of every line.

stripLeading()It returns a string whose value is this string, with all leading white space removed.

stripTrailing()It returns a string whose value is this string, with all trailing white space removed.

subSequence() It returns a character sequence that is a subsequence of this sequence.

substring(int beginIndex)It returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

substring(int beginIndex, int endIndex)It returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex -1.

toCharArray()It converts this string to a new character array.

toLowerCase()It converts all of the characters in this String to lowercase using the rules of the default locale.

toLowerCase(Locale locale)It converts all of the characters in this String to lowercase using the rules of the given Locale.

toString()This object (which is already a string!) is itself returned.

toUpperCase()This method converts all of the characters in this String to uppercase using the rules of the default locale.

toUpperCase(Locale locale)This method converts all of the characters in this String to uppercase using the rules of the given Locale. Case mapping is based on the Unicode Standard version specified by the Character class.

translateEscapes()This method returns a string whose value is this string, with escape sequences translated as if in a string literal.

trim()This method returns a string whose value is this string, with all leading and trailing space removed.

String.valueOf(boolean b)This method returns the string representation of the boolean argument.

String.valueOf(char c) This method returns the string representation of the char argument.

String.valueOf(char[] data) This method returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.

String.valueOf(char[] data, int offset, int count)his method returns the string representation of a specific subarray of the char array argument.

String.valueOf(double d)This method returns the string representation of the double argument.

String.valueOf(float f)This method returns the string representation of the float argument.

String.valueOf(int i)This method returns the string representation of the int argument.

String.valueOf(long l)This method returns the string representation of the long argument.

String indexOf() int in Java

indexOf(): This method is available in java.lang.String class of Java.

Syntax:

int java.lang.String.indexOf(int ch)

This method takes one argument of type int as its parameter. This method returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned.

Parameters: One parameter is required for this method.

ch: a character (Unicode code point).

Returns: the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

For Example:

String str = "Hello"

int ch = 'e'

str.indexOf(ch) = > It returns 1.

Approach

Java

public class IndexOfInt {
    public static void main(String[] args) {

        String str = "Hello";
        int ch = 'e';

        System.out.println(str.indexOf(ch));
    }
}

Output:

1


Some more methods of String

charAt() It returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

codePointAt()It returns the character (Unicode code point) at the specified index. The index refers to char values(Unicode code units) and ranges from 0 to length() - 1.

codePointBefore()It returns the character (Unicode code point) before the specified index.

codePointCount()It returns the number of Unicode code points in the specified text range of this String.

compareTo()It compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

compareToIgnoreCase()It compares two strings lexicographically, ignoring case differences.

concat()It concatenates the specified string to the end of this string.

contains()It returns true if and only if this string contains the specified sequence of char values.

contentEquals(CharSequence cs): It compares this string to the specified CharSequence.

contentEquals(StringBuffer sb) It compares this string to the specified StringBuffer.

String.copyValueOf(char[] data)It returns the String representation of the char array characters.

String.copyValueOf(char[] data, int offset, int count) It returns the string that contains the character of the specified sub-array.

endsWith()It tests if this string ends with the specified suffix.

equals()It compares this string to the specified object.

equalsIgnoreCase()It Compares this String to another String, ignoring case considerations.

String.format()It returns a formatted string using the specified format string and arguments.

formatted()It formats using this string as the format string, and the supplied arguments.

String.format(Locale l, String format, Object... args): It returns a formatted string using the specified locale, format string, and arguments.

getBytes()It encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

getBytes(Charset charset): It encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. 

getBytes(String charsetName)It encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

hashCode()It returns a hash code for this string.

indent()It adjusts the indentation of each line of this string based on the value of n and normalizes line termination characters.

indexOf(int ch)It returns the index within this string of the first occurrence of the specified character. 

indexOf(int ch, int fromIndex)It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

indexOf(String str)It returns the index within this string of the first occurrence of the specified substring.

indexOf(String str, int fromIndex)It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

intern()It returns a canonical representation for the string object.

isBlank()It returns true if the string is empty or contains only white space code points, otherwise false.

isEmpty()It returns true if, and only if, length() is 0.

String.join(): It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

lastIndexOf(int ch) It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(int ch, int fromIndex)It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(String str)It returns the index within this string of the last occurrence of the specified substring.

lastIndexOf(String str, int fromIndex): It returns the index within this string of the last occurrence of the specified substring.

length()It returns the length of this string.

matches()It tells whether or not this string matches the given regular expression.

offsetByCodePoints():It returns the index within this String that is offset from the given index by codePointOffset code points.

regionMatches(int toffset, String other, int ooffset, int len)It tests if two string regions are equal.

regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)It tests if two string regions are equal. A substring of this String object is compared to a substring of the argument other.

repeat()It returns a string whose value is the concatenation of this string repeated count times.

replaceAll()It replaces each substring of this string that matches the given regular expression with the given replacement.

replace(char oldChar, char newChar) It returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)It replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst()It replaces the first substring of this string that matches the given regular expression with the given replacement.

split(String regex)It splits this string around matches of the given regular expression.

split(String regex, int limit)It splits this string around matches of the given regular expression.

startsWith(String prefix)It tests if this string starts with the specified prefix.

startsWith(String prefix, int offset)It tests if the substring of this string beginning at the specified index starts with the specified prefix.

strip()It returns a string whose value is this string, with all leading and trailing white space removed.

stripIndent()It returns a string whose value is this string, with incidental white space removed from the beginning and end of every line.

stripLeading()It returns a string whose value is this string, with all leading white space removed.

stripTrailing()It returns a string whose value is this string, with all trailing white space removed.

subSequence() It returns a character sequence that is a subsequence of this sequence.

substring(int beginIndex)It returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

substring(int beginIndex, int endIndex)It returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex -1.

toCharArray()It converts this string to a new character array.

toLowerCase()It converts all of the characters in this String to lowercase using the rules of the default locale.

toLowerCase(Locale locale)It converts all of the characters in this String to lowercase using the rules of the given Locale.

toString()This object (which is already a string!) is itself returned.

toUpperCase()This method converts all of the characters in this String to uppercase using the rules of the default locale.

toUpperCase(Locale locale)This method converts all of the characters in this String to uppercase using the rules of the given Locale. Case mapping is based on the Unicode Standard version specified by the Character class.

translateEscapes()This method returns a string whose value is this string, with escape sequences translated as if in a string literal.

trim()This method returns a string whose value is this string, with all leading and trailing space removed.

String.valueOf(boolean b)This method returns the string representation of the boolean argument.

String.valueOf(char c) This method returns the string representation of the char argument.

String.valueOf(char[] data) This method returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.

String.valueOf(char[] data, int offset, int count)his method returns the string representation of a specific subarray of the char array argument.

String.valueOf(double d)This method returns the string representation of the double argument.

String.valueOf(float f)This method returns the string representation of the float argument.

String.valueOf(int i)This method returns the string representation of the int argument.

String.valueOf(long l)This method returns the string representation of the long argument.

String indent() in Java

indent(): This method is available in java.lang.String class of Java.

Syntax:

String java.lang.String.indent(int n)

This method takes one argument of type int as its parameter. This method adjusts the indentation of each line of this string based on the value of n and normalizes line termination characters.

Note:

1. If n > 0 then n spaces (U+0020) are inserted at the beginning of each line.

2. If n < 0 then up to n white space characters are removed from the beginning of each line. If a given line does not contain sufficient white space then all leading white space characters are removed.

3. If n == 0 then the line remains unchanged.

Parameters: One parameter is required for this method.

n: number of leading white space characters to add or remove. 

Returns: string with indentation adjusted and line endings normalized.

For Example:

String str = "Hello"

int n = 2

str.indent(n) = > It returns   Hello.

Approach

Java

public class Indent {
    public static void main(String[] args) {

        String str = "Hello";

        int n = 2;
        System.out.println(str.indent(n));
    }
}

Output:

  Hello


Some more methods of String

charAt() It returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

codePointAt()It returns the character (Unicode code point) at the specified index. The index refers to char values(Unicode code units) and ranges from 0 to length() - 1.

codePointBefore()It returns the character (Unicode code point) before the specified index.

codePointCount()It returns the number of Unicode code points in the specified text range of this String.

compareTo()It compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

compareToIgnoreCase()It compares two strings lexicographically, ignoring case differences.

concat()It concatenates the specified string to the end of this string.

contains()It returns true if and only if this string contains the specified sequence of char values.

contentEquals(CharSequence cs): It compares this string to the specified CharSequence.

contentEquals(StringBuffer sb) It compares this string to the specified StringBuffer.

String.copyValueOf(char[] data)It returns the String representation of the char array characters.

String.copyValueOf(char[] data, int offset, int count) It returns the string that contains the character of the specified sub-array.

endsWith()It tests if this string ends with the specified suffix.

equals()It compares this string to the specified object.

equalsIgnoreCase()It Compares this String to another String, ignoring case considerations.

String.format()It returns a formatted string using the specified format string and arguments.

formatted()It formats using this string as the format string, and the supplied arguments.

String.format(Locale l, String format, Object... args): It returns a formatted string using the specified locale, format string, and arguments.

getBytes()It encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

getBytes(Charset charset): It encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. 

getBytes(String charsetName)It encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

hashCode()It returns a hash code for this string.

indent()It adjusts the indentation of each line of this string based on the value of n and normalizes line termination characters.

indexOf(int ch)It returns the index within this string of the first occurrence of the specified character. 

indexOf(int ch, int fromIndex)It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

indexOf(String str)It returns the index within this string of the first occurrence of the specified substring.

indexOf(String str, int fromIndex)It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

intern()It returns a canonical representation for the string object.

isBlank()It returns true if the string is empty or contains only white space code points, otherwise false.

isEmpty()It returns true if, and only if, length() is 0.

String.join(): It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

lastIndexOf(int ch) It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(int ch, int fromIndex)It returns the index within this string of the last occurrence of the specified character.

lastIndexOf(String str)It returns the index within this string of the last occurrence of the specified substring.

lastIndexOf(String str, int fromIndex): It returns the index within this string of the last occurrence of the specified substring.

length()It returns the length of this string.

matches()It tells whether or not this string matches the given regular expression.

offsetByCodePoints():It returns the index within this String that is offset from the given index by codePointOffset code points.

regionMatches(int toffset, String other, int ooffset, int len)It tests if two string regions are equal.

regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)It tests if two string regions are equal. A substring of this String object is compared to a substring of the argument other.

repeat()It returns a string whose value is the concatenation of this string repeated count times.

replaceAll()It replaces each substring of this string that matches the given regular expression with the given replacement.

replace(char oldChar, char newChar) It returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)It replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst()It replaces the first substring of this string that matches the given regular expression with the given replacement.

split(String regex)It splits this string around matches of the given regular expression.

split(String regex, int limit)It splits this string around matches of the given regular expression.

startsWith(String prefix)It tests if this string starts with the specified prefix.

startsWith(String prefix, int offset)It tests if the substring of this string beginning at the specified index starts with the specified prefix.

strip()It returns a string whose value is this string, with all leading and trailing white space removed.

stripIndent()It returns a string whose value is this string, with incidental white space removed from the beginning and end of every line.

stripLeading()It returns a string whose value is this string, with all leading white space removed.

stripTrailing()It returns a string whose value is this string, with all trailing white space removed.

subSequence() It returns a character sequence that is a subsequence of this sequence.

substring(int beginIndex)It returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

substring(int beginIndex, int endIndex)It returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex -1.

toCharArray()It converts this string to a new character array.

toLowerCase()It converts all of the characters in this String to lowercase using the rules of the default locale.

toLowerCase(Locale locale)It converts all of the characters in this String to lowercase using the rules of the given Locale.

toString()This object (which is already a string!) is itself returned.

toUpperCase()This method converts all of the characters in this String to uppercase using the rules of the default locale.

toUpperCase(Locale locale)This method converts all of the characters in this String to uppercase using the rules of the given Locale. Case mapping is based on the Unicode Standard version specified by the Character class.

translateEscapes()This method returns a string whose value is this string, with escape sequences translated as if in a string literal.

trim()This method returns a string whose value is this string, with all leading and trailing space removed.

String.valueOf(boolean b)This method returns the string representation of the boolean argument.

String.valueOf(char c) This method returns the string representation of the char argument.

String.valueOf(char[] data) This method returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.

String.valueOf(char[] data, int offset, int count)his method returns the string representation of a specific subarray of the char array argument.

String.valueOf(double d)This method returns the string representation of the double argument.

String.valueOf(float f)This method returns the string representation of the float argument.

String.valueOf(int i)This method returns the string representation of the int argument.

String.valueOf(long l)This method returns the string representation of the long argument.