How to get first 10 letters of a strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to get first 10 letters of a strings?

I have a string which length is more than 50. I want to show the first 10 letters and ... . How I can implement it.

29th Dec 2017, 3:42 AM
Shahed Mohammad Hridoy
Shahed Mohammad Hridoy - avatar
3 Answers
+ 17
String str="helloiamGaurav,happynewyear",newstr=""; for(int a=0;a<=9;a++) newstr+=str.charAt(a); System.out.print(newstr); //yes , possibly there are many other possible ways , new methods for that☺
29th Dec 2017, 4:41 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
@Bharath_Teki u could do this String s1="TeamA 20"; String s2="TeamB 40"; int newS1=Integer.parseInt(s1.replaceAll("\\D","")); // using regex to repalace all characters except the numbers int newS2=Integer.parseInt(s2.replaceAll("\\D","")); // same here if(newS1>newS2){ System.out.println("TeamA 20 won"); } else { System.out.println("TeamB 40 won"); }
29th Dec 2017, 5:13 AM
Nikhil Gaurav
Nikhil Gaurav - avatar
0
@ Gaurav given String s1 = " TeamA 20 TeamB 40"; Based on given string,We have to decided which team won. I know that., parseInt() converts string to integer but I am confused.Help me in logic.
29th Dec 2017, 4:46 AM
Bharath_Teki 🇮🇳
Bharath_Teki 🇮🇳 - avatar