Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Substring

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Substring

    I want to write a program which cut letters from the end of a string until the last letter is the same as first. For example:
    "kokota" --> "kok"
    "alinak" --> "alina"
    "kocur" ---> k etc.
    I tried doing it like this:
    import java.util.Scanner;
     
    public class zad681 {
        public static String funkyFunction (String s) {
     
            for(int i = s.length() - 1; i > 0; i--){
                if(s.charAt(i) == s.charAt(0)){
                    s.substring(0, i);
                }
            }
            return s;
        }
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Wprowadź zdanie:");
            String content = scan.nextLine();
            String[] words = content.split("\\s+");
            for(int i = 0; i < words.length; i++){
                System.out.println(zad681.funkyFunction(words[i]));
            }
     
            System.out.println(zad681.funkyFunction("ala ma kotka"));
        }
    }
    And program prints s string so its content isn't modified.
    I think using substring and charAt is good idea. Thank you from your answers.
    Last edited by javastart; August 17th, 2020 at 12:11 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,314
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: Substring

    cut letters from the end of a string until the first letter is the same as frist.
    Can you describe in English what the steps are that the program needs to take to do that?

    Once we agree on what steps the program must take, work on the code to do it.


    Note: The substring method returns a String that needs to be saved. The original String is not changed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    javastart (August 17th, 2020)

  4. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Substring

    Quote Originally Posted by Norm View Post
    Can you describe in English what the steps are that the program needs to take to do that?

    Once we agree on what steps the program must take, work on the code to do it.


    Note: The substring method returns a String that needs to be saved. The original String is not changed.
    I edited post and used your tip to fix my code and it worked. Thank you.

Similar Threads

  1. substring
    By frozen java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2012, 10:10 PM
  2. Need some help with substring please
    By iijakeh in forum Java SE APIs
    Replies: 18
    Last Post: April 7th, 2012, 08:08 AM
  3. Sorting substring?
    By alpvii in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 23rd, 2011, 05:39 AM
  4. SUBSTRING
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 1
    Last Post: November 12th, 2010, 03:57 AM
  5. Substring help
    By Roach in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2010, 07:57 AM

Tags for this Thread