DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Need Help

  1. #1
    Join Date
    Jul 2005

    Need Help

    Hi guys.
    I am new at Java, and I have stupid assignment:
    Print the index (position) of the character ‘S’ (in “Spring”) of the 2nd string.
    the second string (s2) is “One flower opens, Spring everywhere”.
    May be some of you will have time and help me.
    Any help would be great.
    Thank you.

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    It doesn't seem stupid to me ...

    Do you need to determine whether the entirety of the first string is a substring in the second string? Or just whether string1[0] is present somewhere in string2 and, if so, at what position?

    If the entirety of the string must be in string2, have you looked at some of the String class methods? Take a look at the two-argument version of startsWith() ... can you think of a way to use this method to determine the "toffset" which is the index of the first letter of string 1 within string 2, if it is contained in string 2?

    Or you could go the easy way and use string2.indexOf( String1 ).

    If you just have to find whether the first character is in the second string, use string2.indexOf( string1.charAt[0] ).

    Happy hunting!!

  3. #3
    Join Date
    Jul 2005
    the Netherlands
    Like nspils said, but here a little code snippet:

    public class Test  {
        public static void main(String[] args) {
            char c = 'S';
            String s = "One flower opens, Spring everywhere";
            int pos = s.indexOf(c)+1;
            System.out.println("Position of '"+c+"' in \""+s+"\" is "+pos+".");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.