Sunday, September 3, 2017

Java - Strings Class

Source Link:
https://www.tutorialspoint.com/java/java_strings.htm

Some Useful Methods:

char charAt(int index)
Returns the character at the specified index.

String concat(String str)
Concatenates the specified string to the end of this string.

boolean equals(Object anObject)
Compares this string to the specified object.

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

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.

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

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

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

int lastIndexOf(String str)
Returns the index within this string of the rightmost occurrence of the specified substring.

int length()
Returns the length of this string.

String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

String[] split(String regex)
Splits this string around matches of the given regular expression.

boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.

String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.

char[] toCharArray()
Converts this string to a new character array.

String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.

String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.

String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.

Example:
import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("   Welcome to Tutorialspoint.com   ");

      System.out.print("Return Value :" );
      System.out.println(Str.trim() );
   }
}
This will produce the following result −
Return Value :Welcome to Tutorialspoint.com

No comments:

Post a Comment

Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments

Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...