JavaRush /Java Blog /Random EN /Reference for java. Static strings
articles
Level 15

Reference for java. 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 string, you cannot change the characters and their number. In addition to the standard creation with 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 benefits of separating strings into static and dynamic ones is to improve security where strings are used as arguments (eg, opening databases, internet connections, class loading mechanism).

operation +

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

substring extraction

There is a note about the substring method - the returned string uses the same byte array as the original one. For example, you loaded string A from a 1mb file. They found something there and highlighted it in a separate line B with a length of 3 characters. Line B in reality also occupies 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) - lexigraphic comparison of strings case-insensitive;
  • regionMatches (boolean ignoreCase, int tooffset, String other, int ooffset, int len) - test for the identity of sections of strings, case-sensitive characters can be specified;
  • regionMatches (int toffset, String other, int ooffset, int len) - test for identity of sections of strings;
  • concat (String str) - returns the concatenation of two strings;
  • contains (CharSequence s) - checks if the specified sequence of characters is included in the string;
  • endsWith (String suffix) - checks if a string ends with the specified suffix;
  • startsWith (String prefix) - checks if the string starts with the specified prefix;
  • startsWith (String prefix, int toffset) - checks if the string at the specified position starts with the specified prefix;
  • equals (Object anObject) - checks if the string is identical to the specified object;
  • getBytes () - returns the byte representation of a 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) - search 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) - search for the first occurrence of the specified substring from the specified position;
  • lastIndexOf (int ch) - search for the last occurrence of a character;
  • 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) - search 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) - replacement of one substring with another;
  • substring (int beginIndex, int endIndex) - return a substring as a string;
  • toLowerCase () - convert string to lowercase;
  • toLowerCase (Locale locale) - convert the string to lower case using the specified locale;
  • toUpperCase() - convert string to uppercase;
  • toUpperCase (Locale locale) - convert string to uppercase using the specified locale;
  • trim() - trim empty characters at the ends of a string;
  • valueOf(a) - static methods for converting various types to a string.
The search methods return the index of the occurrence, or -1 if the search is not found. Conversion methods like replace do not change the string itself, but return the 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 match 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 by the limit value. If limit>0, then the size of the returned array of strings will not be greater than 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