JavaRush /Java Blog /Random EN /Java reference. Static strings
articles
Level 15

Java reference. Static strings

Published in the Random EN group
Strings in Java are described by the String class and are static, i.e. In an existing line, you cannot change the characters and their number. In addition to the standard creation by the new operator, strings can be created directly from a string literal. At the same time, for optimization purposes, objects created in this way are additionally stored in a separate area - the string pool. String s1 = "d" // строка будет сохранена в пуле // строка не будет сохранена в пуле // будет уничтожена сборщиком мусора String s2 = new String("a"); One of the advantages of separating strings into static and dynamic is that it increases security where strings are used as arguments (for example, opening databases, Internet connections, class loading mechanisms).

operation +

The + operation is available for strings, allowing you to combine several strings into one. If one of the operands is not a string, it is automatically converted to a string. For objects, the toString method is used for this purpose. Each operation internally uses a dynamic StringBuilder or StringBuffer object. Therefore, to assemble a string from several, it is still optimal to use one StringBuilder/StringBuffer at once.

substring selection

There is a note about the substring method - the returned string uses the same byte array as the original one. For example, you downloaded line A from a 1MB file. They found something there and separated it into a separate line B, 3 characters long. Line B in reality also takes up the same 1MB. String s ="very .... long string from file"; // совместно использует ту же память что и s String sub1 = s.substring(2,4); // этот an object использует отдельный массив на 4 символа String sub2 = new String(s.substring(2,4));

basic methods

  • compareTo (String anotherString) - lexigraphic comparison of strings;
  • compareToIgnoreCase (String str) - case-insensitive lexigraphic comparison of strings;
  • regionMatches (boolean ignoreCase, int toffset, String other, int ooffset, int len) - test for the identity of string regions, you can specify case sensitivity;
  • regionMatches (int toffset, String other, int ooffset, int len) - test for the identity of string regions;
  • concat (String str) - returns the concatenation of two strings;
  • contains (CharSequence s) - checks whether the specified sequence of characters is included in the string;
  • endsWith (String suffix) - checks whether the string ends with the specified suffix;
  • startsWith (String prefix) - checks whether the string starts with the specified prefix;
  • startsWith (String prefix, int toffset) - checks whether the string at the specified position starts with the specified prefix;
  • equals (Object anObject) - checks whether the string is identical to the specified object;
  • getBytes () - returns the byte representation of the string;
  • getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin) - returns a character representation of a section of a string;
  • hashCode () - hash code of the string;
  • indexOf (int ch) - search for the first occurrence of a character in a string;
  • indexOf (int ch, int fromIndex) - searches for the first occurrence of a character in a string from the specified position;
  • indexOf (String str) - search for the first occurrence of the specified substring;
  • indexOf (String str, int fromIndex) - searches for the first occurrence of the specified substring from the specified position;
  • lastIndexOf (int ch) - search for the last occurrence of a symbol;
  • lastIndexOf (int ch, int fromIndex) - search for the last occurrence of a character from the specified position;
  • lastIndexOf (String str) - search for the last occurrence of a string;
  • lastIndexOf (String str, int fromIndex) - searches for the last occurrence of a string from the specified position;
  • replace (char oldChar, char newChar) - replacing one character in a string with another;
  • replace (CharSequence target, CharSequence replacement) - replacing one substring with another;
  • substring (int beginIndex, int endIndex) - return substring as a string;
  • toLowerCase () - convert a string to lowercase;
  • toLowerCase (Locale locale) - convert a string to lowercase using the specified locale;
  • toUpperCase() - convert the string to uppercase;
  • toUpperCase (Locale locale) - convert a string to uppercase using the specified locale;
  • trim() - trim empty characters at the ends of the string;
  • valueOf(a) - static methods for converting various types to a string.
Search methods return the index of the occurrence or -1 if the searched item is not found. Conversion methods like replace do not modify the string itself but return a corresponding new string object. methods with regular expressions Strings also have a number of methods using regular expressions :
  • matches (String regex) - whether the string matches the specified regular expression;
  • replaceAll (String regex, String rplс) - replaces all occurrences of strings that satisfy the regular expression with the specified string;
  • replaceFirst (String regex, String rplс) - replaces the first occurrence of a string that matches the regular expression with the specified string;
  • split (String regex) - splits a string into parts, the boundaries of the split are occurrences of strings that satisfy the regular expression;
  • split (String regex, int limit) - similar to the previous one, but with the limitation of applying the regular expression to the string to the limit value. If limit>0, then the size of the returned string array will not exceed limit. If limit<=0, then the regular expression is applied to the string an unlimited number of times.
Link to the original source: http://darkraha.com/rus/java/lang/string/static_string.php
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION