Introduction for String
String ये Java के लिए एक 'Non-primitive' Data Type होता है | String; एक से अधिक characters का group होता है | String को double quotes(" ") के अन्दर लिखा जाता है |String ये class; java.lang इस package में define होता है | C और C++ में String के लिए character data type और array का इस्तेमाल किया जाता था | लेकिन Java में String के object को create किया जाता है |
Java में भी C और C++ के जैसे String को create किया जाता है |
For Example
Source Code :public class Sample{
public static void main(String args[]){
char str1[] = {'H','e','l','l','o',' ','W','o','r','l','d'};
String s = new String(str1);
//both are same.
String str2 = "Hello World";
System.out.println("String 1 : " + s);
System.out.println("String 2 : " + str2);
}
}
Output:
String 1 : Hello World String 2 : Hello World
String Literal double quotes(" ") में close होता है |
For eg.
String str = "Hello World";
String Object; Store कैसे होता है ?
जब String object के साथ string literal इस्तेमाल होता है, तब string literal; string pool पर store होता है |String Pool ये String Object को store करने के ख़ास memory है |
for eg.
String str1 = "Hello World";


निचे दिए हुए program में string pool पर दो अलग-अलग object store किये गए है |
for eg.
String str1 = "Hello"; String str2 = "Hello World";


एक जैसे String के साथ जब दो object बनाये जाते है, तब दोनों object को एक ही string literal को string pool से return किया जाता है |
for eg.
String str1 = "Hello World"; String str2 = "Hello World";


Java String Methods
| Return type and String Methods | Description |
|---|---|
| boolean contentEquals(StringBuffer strbuff) | String से string buffer को compare किया जाता है और ये method boolean value को return करता है | |
| boolean endsWith(String suffix) | String में end के suffix को boolean value में return किया जाता है | |
| boolean equals(Object obj) | दो string को compare किया जाता है और boolean value; return होती है | |
| boolean equalsIgnoreCase(String str) | दो string को compare किया जाता है और boolean value; return होती है | |
| boolean matches(String regex) | String दिए हुए regular expression से match हो रहा है या नहीं हो रहा है इसके हिसाब से boolean value; return होती है | |
| boolean regionMatches(boolean ignoreCase, int startIndex, String str, int str_startIndex, int len) | दो string या string के substring को compare किया जाता है | |
| boolean regionMatches(int startIndex, String str, int str_StartIndex, int len) | दो string या string के substring को compare किया जाता है | |
| boolean startsWith(String prefix) | string prefix को check करके boolean value; return की जाती है | |
| boolean startsWith(String prefix, int startIndex) | string prefix को check करके boolean value; return की जाती है | |
| byte[] getBytes() | string से byte array को return किया जाता है | |
| char charAt(int index) | दिए हुए index पर जो character है उसे return करता है | |
| char[] toCharArray() | string को character array में convert किया जाता है | |
| CharSequence subSequence(int beginIndex, int endIndex) | character sequence को return किया जाता है | |
| int compareTo(Object obj) | String को किसी दुसरे object से compare किया जाता है | |
| int compareTo(String str2) | String को किसी दुसरे string से compare किया जाता है | |
| int compareToIgnoreCase(String str2) | String को किसी दुसरे string से compare किया जाता है | |
| int indexOf(String str) | string के character या substring का index; integer में return किया जाता है | |
| int indexOf(String str, int fromIndex) | starting Index से string के character या substring का index; integer में return किया जाता है | |
| int indexOf(int ch) | string के character का index; integer में return किया जाता है | |
| int indexOf(int ch, int fromIndex) | starting Index से string के character या substring का index; integer में return किया जाता है | |
| int lastIndexOf(String str) | string या substring का index; integer में return किया जाता है | |
| int lastIndexOf(String str, int fromIndex) | starting Index से string के character या substring का index; integer में return किया जाता है | |
| int lastIndexOf(int ch) | string के character का index; integer में return किया जाता है | |
| int lastIndexOf(int ch, int fromIndex) | starting Index से string के character का index; integer में return किया जाता है | |
| int length() | string की length को integer में return किया जाता है | |
| void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin) | string; character array पर copy किया जाता है | |
| static String copyValueOf(char[] data) | character array; string को return किया जाता है | |
| static String copyValueOf(char[] data, int offset, int count) | character array; string को return किया जाता है | |
| static String valueOf(basic data type) | character array; string को return किया जाता है | |
| String concat(String str) | अलग-अलग types को string में convert किया जाता है | |
| String intern() | string का canonical representation return करता है | |
| String replace(char oldChar, char newChar) | string के दिए हुए character से replace किया जाता है | |
| String replaceAll(String regex, String replacement) | string को regular expression से replace कर देता है | |
| String replaceFirst(String regex, String replacement) | string को regular expression से replace कर देता है | |
| String substring(int beginIndex) | string से substring को बनाया जाता है | |
| String substring(int beginIndex, int endIndex) | string से substring को बनाया जाता है | |
| String toLowerCase() | सभी uppercase letters को lowercase में convert कर देता है | |
| String toUpperCase() | सभी lowercase letters को uppercase में convert कर देता है | |
| String trim() | ुरुआत के और आखिर के whitespaces को remove किये जाते है | |
| String[] split(String regex) | string के regular expression से अलग-अलग substring बनाकर उस string का array; return किया जाता है | |
| String[] split(String regex, int limit) | string के regular expression से अलग-अलग substring बनाकर उस string का array; return किया जाता है |







No comments: