Reťazec je postupnosť znakov. V Jave sú objekty String nemenné, čo znamená konštantu a po vytvorení sa nedajú zmeniť.
Vytvorenie reťazca
Existujú dva spôsoby, ako vytvoriť reťazec v jazyku Java:
1. Reťazový doslov
String s = techcodeview.com;>
2. Používanie Nový kľúčové slovo
String s = new String (techcodeview.com);>
Konštruktory reťazcov v jazyku Java
1. Reťazec(byte[] byte_arr)
Vytvorte nový reťazec dekódovaním bajtové pole . Na dekódovanie používa predvolenú znakovú sadu platformy.
Príklad:
byte[] b_arr = {71, 101, 101, 107, 115}; String s_byte =new String(b_arr); //Geeks>
2. String(byte[] byte_arr, Charset char_set)
Vytvorte nový reťazec dekódovaním bajtové pole . Používa sa char_set na dekódovanie.
Príklad:
byte[] b_arr = {71, 101, 101, 107, 115}; Charset cs = Charset.defaultCharset(); String s_byte_char = new String(b_arr, cs); //Geeks>
3. String(byte[] byte_arr, String char_set_name)
Vytvorte nový reťazec dekódovaním bajtové pole . Používa sa char_set_name na dekódovanie. Vyzerá podobne ako vyššie uvedené konštrukcie a objavujú sa pred podobnými funkciami, ale vyžaduje to Reťazec (ktorý obsahuje char_set_name) ako parameter, kým vyššie uvedený konštruktor berie CharSet.
Príklad:
byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 'US-ASCII'); //Geeks>
4. String(byte[] byte_arr, int start_index, int length)
Zostavte nový reťazec z pole bajtov záležiac na start_index (východiskové miesto) a dĺžka (počet znakov od počiatočného miesta).
Príklad:
byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 1, 3); // eek>
5. String(byte[] byte_arr, int start_index, int length, Charset char_set)
Zostavte nový reťazec z pole bajtov záležiac na start_index (východiskové miesto) a dĺžka (počet znakov od počiatočného miesta) .Používa char_set na dekódovanie.
Príklad:
java matematika.min
byte[] b_arr = {71, 101, 101, 107, 115}; Charset cs = Charset.defaultCharset(); String s = new String(b_arr, 1, 3, cs); // eek>
6. String(byte[] byte_arr, int start_index, int length, String char_set_name)
Zostavte nový reťazec z pole bajtov záležiac na start_index (východiskové miesto) a dĺžka (počet znakov od počiatočného miesta) .Používa char_set_name na dekódovanie.
Príklad:
byte[] b_arr = {71, 101, 101, 107, 115}; String s = new String(b_arr, 1, 4, 'US-ASCII'); // eeks>
7. String(char[] char_arr)
Pridelí nový reťazec z daného Pole znakov
Príklad:
char char_arr[] = {'G', 'e', 'e', 'k', 's'}; String s = new String(char_arr); //Geeks>
8. String(char[] char_array, int start_index, int count)
Alokuje reťazec z daného pole znakov ale vyber si počítať postavy z start_index .
Príklad:
char char_arr[] = {'G', 'e', 'e', 'k', 's'}; String s = new String(char_arr , 1, 3); //eek>
9. String(int[] uni_code_points, int offset, int count)
Alokuje reťazec z a uni_code_array ale vyber si počítať postavy z start_index .
Príklad:
int[] uni_code = {71, 101, 101, 107, 115}; String s = new String(uni_code, 1, 3); //eek>
10. Reťazec (StringBuffer s_buffer)
Pridelí nový reťazec z reťazca in s_buffer
Príklad:
večera vs večera
StringBuffer s_buffer = new StringBuffer('Geeks'); String s = new String(s_buffer); //Geeks>
11. Reťazec(StringBuilder s_builder)
Pridelí nový reťazec z reťazca in s_builder
Príklad:
StringBuilder s_builder = new StringBuilder('Geeks'); String s = new String(s_builder); //Geeks>
Reťazcové metódy v Jave
1. int dĺžka()
Vráti počet znakov v reťazci.
java previesť znak na reťazec
'techcodeview.com'.length(); // returns 13>
2. Char charAt (int i)
Vráti znak na ithindex.
'techcodeview.com'.charAt(3); // returns ‘k’>
3. Podreťazec reťazca (int i)
Vráťte podreťazec z ithindexový znak na koniec.
'techcodeview.com'.substring(3); // returns ksforGeeks>
4. Podreťazec reťazca (int i, int j)
Vráti podreťazec od i do j-1 indexu.
'techcodeview.com'.substring(2, 5); // returns eks>
5. String concat( String str)
Spája zadaný reťazec na koniec tohto reťazca.
String s1 = Geeks; String s2 = forGeeks; String output = s1.concat(s2); // returns techcodeview.com>
6. int indexOf (String s)
Vráti index v reťazci prvého výskytu zadaného reťazca.
Ak reťazec s nie je prítomný vo vstupnom reťazci, vráti sa -1 ako predvolená hodnota.
1. String s = Learn Share Learn; int output = s.indexOf(Share); // returns 6 2. String s = 'Learn Share Learn' int output = s.indexOf(Play); // return -1>
7. int indexOf (reťazec s, int i)
Vráti index v reťazci prvého výskytu zadaného reťazca počnúc zadaným indexom.
String s = Learn Share Learn; int output = s.indexOf('ea',3);// returns 13>
8. Int lastIndexOf( String s)
Vráti index v reťazci posledného výskytu zadaného reťazca.
Ak reťazec s nie je prítomný vo vstupnom reťazci, vráti sa -1 ako predvolená hodnota.
1. String s = Learn Share Learn; int output = s.lastIndexOf('a'); // returns 14 2. String s = 'Learn Share Learn' int output = s.indexOf(Play); // return -1>
9. boolean rovná sa (objekt inýObj)
Porovná tento reťazec so zadaným objektom.
Boolean out = Geeks.equals(Geeks); // returns true Boolean out = Geeks.equals(geeks); // returns false>
10. boolean equalsIgnoreCase (reťazec inýString)
Porovnáva reťazec s iným reťazcom, pričom ignoruje úvahy o veľkosti písmen.
Boolean out= Geeks.equalsIgnoreCase(Geeks); // returns true Boolean out = Geeks.equalsIgnoreCase(geeks); // returns true>
11. int porovnaťS(String otherString)
Lexikograficky porovnáva dva reťazce.
int out = s1.compareTo(s2); // where s1 and s2 are // strings to be compared This returns difference s1-s2. If : out <0 // s1 comes before s2 out = 0 // s1 and s2 are equal. out>0 // s1 nasleduje po s2.>
12. int CompareToIgnoreCase( String otherString)
Lexikograficky porovnáva dva reťazce, pričom ignoruje úvahy o veľkostiach písmen.
int out = s1.compareToIgnoreCase(s2); // where s1 and s2 are // strings to be compared This returns difference s1-s2. If : out <0 // s1 comes before s2 out = 0 // s1 and s2 are equal. out>0 // s1 nasleduje po s2.>
Poznámka: V tomto prípade nebude brať do úvahy veľkosť písmen (bude ignorovať, či ide o veľké alebo malé písmená).
13. Reťazec na malé písmená()
Skonvertuje všetky znaky v reťazci na malé písmená.
String word1 = HeLLo; String word3 = word1.toLowerCase(); // returns hello'>
14. String toUpperCase()
Skonvertuje všetky znaky v reťazci na veľké písmená.
String word1 = HeLLo; String word2 = word1.toUpperCase(); // returns HELLO>
pätnásť. Orezanie reťazca()
Vráti kópiu reťazca odstránením medzier na oboch koncoch. Nemá vplyv na biele miesta v strede.
String word1 = Learn Share Learn ; String word2 = word1.trim(); // returns Learn Share Learn>
16. Nahradenie reťazca (char oldChar, char newChar)
Vráti nový reťazec nahradením všetkých výskytov z starýChar s novýChar.
String s1 = feeksforfeeks; String s2 = feeksforfeeks.replace(‘f’ ,’g’); // return geeksforgeeks>
Poznámka: s1 je stále feksforfeeks a s2 je geeksgorgeeks
17. boolean obsahuje (reťazec):
Vráti hodnotu true, ak reťazec obsahuje daný reťazec
String s1='geeksforgeeks'; String s2='geeks'; s1.contains(s2) // return true>
18. Char[] toCharArray():
Skonvertuje tento reťazec na nové pole znakov.
15 zo 100,00
String s1='geeksforgeeks'; char []ch=s1.toCharArray(); // returns [ 'g', 'e' , 'e' , 'k' , 's' , 'f', 'o', 'r' , 'g' , 'e' , 'e' , 'k' ,'s' ]>
19. booleovské hviezdyWith(reťazec):
Ak reťazec začína touto predponou, vráti hodnotu true.
String s1='geeksforgeeks'; String s2='geeks'; s1.startsWith(s2) // return true>
Príklad String Constructor a String Methods
Nižšie je uvedená implementácia vyššie uvedenej témy:
Java // Java code to illustrate different constructors and methods // String class. import java.io.*; import java.util.*; // Driver Class class Test { // main function public static void main (String[] args) { String s= 'techcodeview.com'; // or String s= new String ('techcodeview.com'); // Returns the number of characters in the String. System.out.println('String length = ' + s.length()); // Returns the character at ith index. System.out.println('Character at 3rd position = ' + s.charAt(3)); // Return the substring from the ith index character // to end of string System.out.println('Substring ' + s.substring(3)); // Returns the substring from i to j-1 index. System.out.println('Substring = ' + s.substring(2,5)); // Concatenates string2 to the end of string1. String s1 = 'Geeks'; String s2 = 'forGeeks'; System.out.println('Concatenated string = ' + s1.concat(s2)); // Returns the index within the string // of the first occurrence of the specified string. String s4 = 'Learn Share Learn'; System.out.println('Index of Share ' + s4.indexOf('Share')); // Returns the index within the string of the // first occurrence of the specified string, // starting at the specified index. System.out.println('Index of a = ' + s4.indexOf('a',3)); // Checking equality of Strings Boolean out = 'Geeks'.equals('geeks'); System.out.println('Checking Equality ' + out); out = 'Geeks'.equals('Geeks'); System.out.println('Checking Equality ' + out); out = 'Geeks'.equalsIgnoreCase('gEeks '); System.out.println('Checking Equality ' + out); //If ASCII difference is zero then the two strings are similar int out1 = s1.compareTo(s2); System.out.println('the difference between ASCII value is='+out1); // Converting cases String word1 = 'GeeKyMe'; System.out.println('Changing to lower Case ' + word1.toLowerCase()); // Converting cases String word2 = 'GeekyME'; System.out.println('Changing to UPPER Case ' + word2.toUpperCase()); // Trimming the word String word4 = ' Learn Share Learn '; System.out.println('Trim the word ' + word4.trim()); // Replacing characters String str1 = 'feeksforfeeks'; System.out.println('Original String ' + str1); String str2 = 'feeksforfeeks'.replace('f' ,'g') ; System.out.println('Replaced f with g ->' + str2); } }>
Výkon
String length = 13 Character at 3rd position = k Substring ksforGeeks Substring = eks Concatenated string = techcodeview.com Index of Share 6 Index of a = 8 Checking Equality false Checking Equality ...>
Pre Set – 2 sa môžete obrátiť na: Java.lang.String trieda v jazyku Java | Súprava 2
Do tohto článku prispeli Rahul Agrawal a naši užitoční používatelia.