0

How to separate elements of a string variable in java? added an extra question

Given a string, return true if it ends in "ly". endsLy("oddly") → true endsLy("y") → false endsLy("oddy") → false

25th Jan 2017, 7:09 AM
Mubeen Sal
Mubeen Sal - avatar
5 Antworten
+ 6
What language? The general idea in C++ would be to get string size, copy string to char array, utilise info on string size to get the last two array index, evaluate the last two array if they correspond to ly. If it's Ruby, you get to do negative array indexes which simplifies a lot of work.
25th Jan 2017, 7:28 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
The method you want to use is called substring, it can be use to return the last two characters of your string and then you can test it in a condition. Example: (Javascript) var str1 = "oddly"; var str2 = str1.substr(-2); if(str2=="ly"){ //do something }else{ //do something };
25th Jan 2017, 7:29 AM
Ghauth Christians
Ghauth Christians - avatar
+ 2
String s; s=in.readLine(); String s1;String s2="ly"; int l=length(); s1=s.substring(l-2,l); if(s1.equalsIgnoreCase(s2)) //now write the statement
25th Jan 2017, 8:01 AM
Vaibhav Tandon
Vaibhav Tandon - avatar
0
Java
25th Jan 2017, 7:30 AM
Mubeen Sal
Mubeen Sal - avatar
0
thanks a lot. what does substring (); do?. actually i wanted to separate all the elements of a string and store it to a character array maybe. I'm a beginner, sorry i asked a simple question
25th Jan 2017, 8:06 AM
Mubeen Sal
Mubeen Sal - avatar