An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
February 21, 2022 07:27 am GMT
Original Link: https://dev.to/souravsingpardeshi/2-basic-string-operations-56nj
2. Basic String operations
Declaring string and assigning value to it
public class main{ public static void main(String[] args){ String str1 = " hello world"; System.out.println(str1); }}
Concatination of strings
public class main{ public static void main(String[] args){ String str1 = " hello world"; String str2 = "India"; String str3 = str1+str2; System.out.println(str3); }}
Accessing element from its position
public class main{ public static void main(String[] args){ String str1 = " hello world"; //charAt(pos) is used to access element in string at pos position. System.out.println(str1.charAt(2)); }}
Getting substring from string
public class main{ public static void main(String[] args){ String str1 = " hello world"; //substring get substring from a string by accepting starting and ending position. System.out.println(str1.substring(2,4)); }}
Using split method
public class main{ public static void main(String[] args){ String str1 = "A,B,C,D"; String str2 = "H I J k"; //will split string by using , as identifier String [] arr1 = str1.split(","); // will split string by using space as identifier String [] arr2 = str2.split(" "); }}
String inbuilt methods
public class main{ public static void main(String[] args){ String str1 = " hello world"; // will return length of string System.out.println(str1.length()); // will convert all char to uppercase System.out.println(str1.toUpperCase()); //check if string starts with "h" System.out.println(str1.startsWith("h")); //check if string ends with "h" System.out.println(str1.endsWith("h")); //will find index of "ll" System.out.println(str1.indexOf("ll")); // will return index of last occurence if "hello" System.out.println(str1.lastIndexOf("hello")); //will replace hello with bye System.out.println(str1.replace("hello", "bye")); System.out.println(str1); System.out.println(Integer.toBinaryString(a)); System.out.println(Integer.toHexString(a)); System.out.println(Integer.parseInt(Integer.toString(a))); }}
Original Link: https://dev.to/souravsingpardeshi/2-basic-string-operations-56nj
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To