Strings Flashcards

1
Q

Constructor

A
  • String(char[] value)

Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

  • String(char[] value, int offset, int count)

Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray.

  • String(byte[] bytes)

Constructs a new String by decoding the specified array of bytes using the platform’s default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

  • String(StringBuilder builder)

Allocates a new string that contains the sequence of characters currently contained in the string builder argument. This constructor is provided to ease migration to StringBuilder. Obtaining a string from a string builder via the toString method is likely to run faster and is generally preferred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

compareTo/compareToIgnoreCase

A
  • compareTo(String anotherString)

Compares two strings lexicographically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

valueOf

A
  • valueOf(boolean b)

Returns string representation of boolean/int/long/float/double/char/char[] argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

charAt

A
  • charAt(int index)

Returns the char value at the specified index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

indexOf

A
  • indexOf(int ch)

Returns the index within this string of the first occurrence of the specified character.

  • indexOf(int ch, int fromIndex)

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

  • indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring.

  • indexOf(String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

lastIndexOf

A
  • lastIndexOf(int ch)

Returns the index within this string of the last occurrence of the specified character.

  • lastIndexOf(int ch, int fromIndex)

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

  • lastIndexOf(String str)

Returns the index within this string of the last occurrence of the specified substring.

  • lastIndexOf(String str, int fromIndex)

Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Length of String

A

length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

copyValueOf

A
  • copyValueOf(char[] data)

Returns a String that represents the character sequence in the array specified.

  • copyValueOf(char[] data, int offset, int count)

Returns a String that represents the character sequence in the array specified.

  • ​It is equivalent to valueOf(), so use that.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

toCharArray() and trim()

A
  • toCharArray()

Converts this string to a new character array.

  • trim()

Returns a copy of the string, with leading and trailing whitespace omitted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

replace/replaceAll/replaceFirst

A
  • replace(char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

  • replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

  • replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.

  • replaceFirst(String regex, String replacement)

Replaces the first substring of this string that matches the given regular expression with the given replacement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

split

A
  • returns a String array (String[])
  • split(String regex)

Splits this string around matches of the given regular expression.

split(String regex, int limit)

Splits this string around matches of the given regular expression.

  • The following example illustrates how the String.split method can be used to break up a string into its basic tokens:
  • String[] result = “this is a test”.split(“\s”);
  • for (int x=0; xSystem.out.println(result[x]);

prints the following output:

  • this
  • is
  • a
  • test
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

StringJoiner

A
  • StringJoiner(CharSequence delimiter)

Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.

  • StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.

  • The String “[George:Sally:Fred]” may be constructed as follows:

StringJoiner sj = new StringJoiner(“:”, “[”, “]”); sj.add(“George”).add(“Sally”).add(“Fred”); String desiredString = sj.toString();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

concat syntax?

A
  • concat(String str)

Concatenates the specified string to the end of this string.

  • ​If a is null, then a.concat(b) throws a NullPointerException but a += b will treat the original value of a as if it were null.
  • Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects). So the concat() method is more strict in what it accepts.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

contains syntax?

A
  • contains(CharSequence s)

Returns true if and only if this string contains the specified sequence of char values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

startsWith/endsWith

A
  • startsWith(String prefix)

Tests if this string starts with the specified prefix.

startsWith(String prefix, int toffset)

Tests if the substring of this string beginning at the specified index starts with the specified prefix.

  • endsWith(String suffix)

Tests if this string ends with the specified suffix.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

getChars

A
  • getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Copies characters from this string into the destination character array.

17
Q

isEmpty()

A
18
Q

substring/subsequence

A
  • subSequence(int beginIndex, int endIndex)

Returns a new character sequence that is a subsequence of this sequence.

  • substring(int beginIndex)

Returns a new string that is a substring of this string.

  • substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.

19
Q

matches(String regex)

A
  • public boolean matches(String regex)
  • Tells whether or not this string matches the given regular expression.
  • An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression

Pattern.matches(regex, str)

20
Q

String vs Charsequence

A
  • CharSequence is an interface whereas String is a concrete class.
  • java.lang.String is an implementation of this interface
21
Q

StringBuilder Ctor

A
* // usind StringBuilder() constructor
 StringBuilder str = new StringBuilder(); 
 StringBuilder str1 = new StringBuilder("AAAABBBCCCC");
 StringBuilder str2 = new StringBuilder(10);
 StringBuilder str3 = new StringBuilder(str1);

str.append(“GFG”);

 // print string 
 System.out.println("String = " + str.toString());

​EXAMPLES

  • StringBuilder append(String str)
  • StringBuilder append(int n)
  • StringBuilder append(Object obj)
  • StringBuilder strBuilder=new StringBuilder (“Core”); strBuilder.insert(1,”Java”); System.out.println(strBuilder); //CJavaCore
  • StringBuilder strBuilder=new StringBuilder(“Core”); strBuilder.replace( 2, 4, “Java”); System.out.println(strBuilder); //CoJava
  • StringBuilder strBuilder=new StringBuilder(“Core”); strBuilder.delete( 2, 4); System.out.println(strBuilder); //Co
  • StringBuilder strBuilder=new StringBuilder(“Core”); strBuilder.reverse(); System.out.println(strBuilder); //eroC

*

22
Q

StringBuilder Methods

A
  1. StringBuilder append(X x): This method appends the string representation of the X type argument to the sequence.
  2. int capacity(): This method returns the current capacity.
  3. char charAt(int index): This method returns the char value in this sequence at the specified index.
  4. StringBuilder delete(int start, int end): This method removes the characters in a substring of this sequence.
  5. StringBuilder deleteCharAt(int index): This method removes the char at the specified position in this sequence.
  6. void ensureCapacity(int minimumCapacity): This method ensures that the capacity is at least equal to the specified minimum.
  7. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): This method characters are copied from this sequence into the destination character array dst.
  8. int indexOf(): This method returns the index within this string of the first occurrence of the specified substring.
  9. StringBuilder insert(int offset, boolean b): This method inserts the string representation of the booalternatelean argument into this sequence.
  10. StringBuilder insert(): This method inserts the string representation of the char argument into this sequence.
  11. int lastIndexOf(): This method returns the index within this string of the last occurrence of the specified substring.
  12. int length(): This method returns the length (character count).
  13. StringBuilder replace(int start, int end, String str): This method replaces the characters in a substring of this sequence with characters in the specified String.
  14. StringBuilder reverse(): This method causes this character sequence to be replaced by the reverse of the sequence.
  15. void setCharAt(int index, char ch): In this method, the character at the specified index is set to ch.
  16. void setLength(int newLength): This method sets the length of the character sequence.
  17. CharSequence subSequence(int start, int end): This method returns a new character sequence that is a subsequence of this sequence.
  18. String substring(): This method returns a new String that contains a subsequence of characters currently contained in this character sequence.
  19. String toString(): This method returns a string representing the data in this sequence.
  20. void trimToSize(): This method attempts to reduce storage used for the character sequence.
23
Q

StringBuilder/StringBuffer

A
  • StringBuffer is synchronized, StringBuilder is not.