Welcome To Search How
Hello friends welcome again to my blog, friends hum java ke bare main seekh rahe hain or humne apni peechli post main seekha tha Java Variables ke bare main or dosto ab hum seekhenge Java Strings ke bare main.Java String Introduction
Friends hum sabse pehle baat karte hain java string ki dosto Strings character ki sequence hoti hain for example "Java In Hindi" ek string hai. dosto java main strings ko objects ki trah implement kiya jata hai, dosto ye isliye kiya jata hai kyunki strings pe operations perform kiye ja sake, Operations for example 2 strings ko add karna ya compare karna etc, dosto hum strings pe perform kar sakte hain, String ek class hai, hum normal string variables bhi create kar sakte hain or string class ka object bhi create kar sakte hain or dono ke sath hi operations perform kar sakte hain.String myString = "this is a string"; // variable
String myString = new String("This is a string"); // object
Friends jab hum String ko variable or object ki trah use karte hain to inme farak hota hai, dosto jab hum string ko object ki trah use karte hain to jab ek bar object create ho jaye to hum usme koi bhi changes nahi kar sakte hain, lekin dosto jab hum string ko variable ki trah use karte hain usme hum koi bhi changes kar sakte hain.
Friends java hume kuch methods provide karti hai jisse hum object main modification kar sakte hain, lekin dosto jab hum esa karte hain to ek new string object generate hota hai, Original object main koi change nahi aata hai. In sabhi operations ko jaanne ke liye neeche read kare.
String Length
Friends kisi bhi string main jitne characters hote hain vo us string ki length hote hain, friends kisi bhi string ki length pta karne ke liye hum length() method use kar sakte hain, dosto iske liye hum us string par length method call karte hian.String mystring = "hello";
System.out.println(myString.length());
Concatenating String
Friends ab hum baat karte hian concatenating string ki to dosto jab hum kisi 2 string ko jodkar third string banate hain usko hum concatenation bolte hain. esa hum + operator ke through bhi kar sakte hain or concat() method ke through bhi kar sakte hain For example ==>String myString = "This is a string";
String yourString ="This is your string";
String ourString = myString+yourString; // with + operator
ourString = myString.concat(yourString); // with concat() method
System.out.println(ourString);
String Indexing with charAt()
Dosto ab hum string indexing with charAt() ke bare main aapko batate hain, dosto ye method string main se kisi ek method ko extract karne ke liye use kiya jata hai, friends string ki indexing arrays ki trah hoti hai, iska pehla character 0 index pe hota hai isliye dosto hume string ke kisi bhi single character ko access karne main aasani hoti hai, dosto iske liye hum charAt() method use karte hain jo index number hum is method main pass karte hain ye method usi index ke character ko return karta hai.String myString = "hello";
System.out.println(myString.charAt(3));
String Indexing with substring()
Friends sabse pehli baat to ye hai ki hum kisi string main sub string search karne ke liye substring() method use karte hain. dosto is method main hum 2 arguments pass karwate hain, first argument vo index number hota hai jaha se hum string ko access karna chahte hain or dosto second argument vo index number hota hain jaha tak ki string hum access karna chahte hain. friends ye method starting index se lekar ending index tak string return karta hai.String s1 = "hello my name is tips";
System.out.println(s1.substring(5,8));
String Indexing with indexOf()
Friends ab hum baat karte hain string indexing with indexOf() ki dosto isme konsi sub string kis index pe hai ye pta karne ke liye hum indexOf() method use karte hain, dosto is method main ek sub string pass ki jati hai, ye method us sub string ki stating index return karta hai.String s1 = "hello my name is tips";
System.out.println(s1.indexOf("my")); // returns 7
String Comparison with equals()
Friends ab hum baat karte hain string comparison with equals() ki to dosto jab hume koi bhi strings ko campare karna hota hai to hum Equals() method ka use karte hain, dosto agar dono string equal hoti hain to ye method true return karta hai, friends ek string pe is method ko call kiya jata hai or dusri string argument ki trah pass ki jati hai for example ==>String s1 = "hello";
String s2 = "hello";
System.out.println(s1.equals(s2)); // prints true
No comments:
Post a Comment