Muallif
July 13, 2024
169Javada String asosan char tipidagi qiymatlar ketma-ketligini ifodalovchi obyekt hisoblanadi. Belgili massiv esa javadagi String kabi ishlaydi, ya`ni:
char[] ch={'j','a','v','a','t','p','o','i','n','t'}; String s=new String(ch); // Kodimiz String s="javatpoint"; Kabi ishlaydi. Java **String** klassi string ustida **compare()**, **concat()**, **equals()**, **split()**, **length()**, **replace()**, **compareTo()**, **intern()**, **substring()** kabi bir necha funksiyalarni ishlatib bir qator amallarni bajarish imkonini beradi. **java.lang.String** klassi **Serializable**, **Comparable** va **CharSequence** interfeyslarini implement qiladi. **CharSequence** interfeysi belgilar ketma-ketligini ifodalashda ishlatiladi. U **String**, **StringBuffer** va **StringBuilder** klasslari tomonidan implement qilinadi. Bundan kelib chiqadiki, biz Javada stringni shu uch klassdan istalgani bilan yasay olamiz. **String** doimiydir, ya'ni uni o`zgartirib bo`lmaydi. Qachonki biz stringga o'zgartirish kiritsak, yangi instance variable – doimiy qiymat hosil qilinadi. O`zgaruvchan string uchun **StringBuffer** va **StringBuilder** klasslaridan foydalanish mumkin. Biz o`zgarmas(**immutable**) stringlarga keyinroq to`xtalamiz. Avval biz javadagi string haqida to`liq ma'lumot olamiz. | No | Funksiyalar | Ta`rifi | |----|---------------------------------------|------------------------------------------------| | 1 | **charAt(int index)** | ma'lum bir indeks uchun **char** qiymat qaytaradi | | 2 | **length()** | **String**ni uzunligini qaytaradi | | 3 | **static String format(String format, Object … args)** | **String** format qaytaradi | | 4 | **static String format(Locale I, String format, Object …args)** | Berilgan local **string** format qaytaradi | | 5 | **String substring(int beginIndex)** | boshlang'ich indeks uchun | | 6 | **String substring(int beginIndex, int endIndex)** | Boshlang'ich va oxirgi indeks uchun | | 7 | **boolean contains(CharSequence s)** | Ketma-ket kelgan **char** larning true yoki false qiymatlarini qaytaradi | | 8 | **static String join(CharSequence delimiter, CharSequence …elements)** | **String**larni qo'shadi | | 9 | **static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)** | **String**larni qo'shadi | | 10 | **boolean equals(Object another)** | **String** va obyektlarini tekshiradi | | 11 | **boolean isEmpty()** | **String**ni bo'sh ekanini tekshiradi | | 12 | **String concat(String str)** | Muayyan **string**larni birlashtiradi | | 13 | **String replace(char old, char new)** | Belgilangan **char**larni almashtiradi | | 14 | **String replace(CharSequence old, CharSequence new)** | Ketma-ket kelgan **char**'lari almashtiradi | | 15 | **String trim()** | Bo'sh joyni tushirib qoldiradi | | 16 | **String split(String regex)** | **String**larni teng bo'ladi | | 17 | **String split(String regex, int limit)** | **String**larni limit bilan bo'ladi | | 18 | **String intern()** | **String**ni biror bir qiymatda ushlab turadi | | 19 | **int indexOf(int ch)** | **Char**larni aniq indeksini qaytaradi | | 20 | **int indexOf(int ch, int fromIndex)** | **Char**ning boshlanish qiymati bilan aniq indeks qaytaradi | | 21 | **int indexOf(String substring)** | **String**ni qaysi indeksda turganini qaytaradi | | 22 | **int indexOf(String substring, int fromIndex)** | **Substring** ko'rsatkichidan indeks o'rnini qaytaradi | | 23 | **String toLowerCase()** | Harflarni kichigi bilan almashtiradi | | 24 | **String toLowerCase(Locale l)** | **Local** berilgan harflarni kichigiga almashtiradi | | 25 | **String toUpperCase()** | Berilgan harflarni kattasiga almashtiradi | | 26 | **String toUpperCase(Locale l)** | **Local** berilgan harflarni kattasiga almashtiradi | ### Funksiyalarga misollar **Java String contains()** ```java class ContainsExample{ public static void main(String args[]){ String name="what do you know about me"; System.out.println(name.contains("do you know")); System.out.println(name.contains("about")); System.out.println(name.contains("hello")); } }
Java String replace(CharSequence target, CharSequence replacement)
java
Копировать код
public class ReplaceExample2{ public static void main(String args[]){ String s1="my name is khan my name is java"; String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was" System.out.println(replaceString); } }
Java String equalsIgnoreCase()
java
Копировать код
public class EqualsIgnoreCaseExample{ public static void main(String args[]){ String s1="javatpoint"; String s2="javatpoint"; String s3="JAVATPOINT"; String s4="python"; System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same } }
Java String valueOf()
java
Копировать код
public class StringValueOfExample{ public static void main(String args[]){ int value=30; String s1=String.valueOf(value); System.out.println(s1+10);//concatenating string with 10 } }
String charAt()
java
Копировать код
public class CharAtExample{ public static void main(String args[]){ String name="javatpoint"; char ch=name.charAt(4);//to'rtinchi elementini qaytaradi System.out.println(ch); } }
String length()
java
Копировать код
public class LengthExample{ public static void main(String args[]){ String s1="javatpoint"; String s2="python"; System.out.println("string length is: "+s1.length());//javatpoint ning uzunligi 10 ga teng System.out.println("string length is: "+s2.length());// python uzunligi 6 ga teng } }
String format()
java
Копировать код
public class FormatExample{ public static void main(String args[]){ String name="sonoo"; String sf1=String.format("name is %s",name); String sf2=String.format("value is %f",32.33434); String sf3=String.format("value is %32.12f",32.33434); System.out.println(sf1); System.out.println(sf2); System.out.println(sf3); } }
String substring()
java
Копировать код
public class SubstringExample{ public static void main(String args[]){ String s1="javatpoint"; System.out.println(s1.substring(2,4));//va ni qaytaradi System.out.println(s1.substring(2));// vatpoint ni qaytaradi } }
String join()
java
Копировать код
public class StringJoinExample{ public static void main(String args[]){ String joinString1=String.join("-","welcome","to","javatpoint"); System.out.println(joinString1); } }
String equals()
java
Копировать код
public class EqualsExample{ public static void main(String args[]){ String s1="javatpoint"; String s2="javatpoint"; String s3="JAVATPOINT"; String s4="python"; System.out.println(s1.equals(s2));//true System.out.println(s1.equals(s3));//false System.out.println(s1.equals(s4));//false } }
String isEmpty()
java
Копировать код
public class IsEmptyExample{ public static void main(String args[]){ String s1=""; String s2="javatpoint"; System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); } }
String concat()
java
Копировать код
public class ConcatExample{ public static void main(String args[]){ String s1="java string"; s