Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:42:49+00:00 2026-06-04T13:42:49+00:00

I am trying to trim a string to the first occurrence of a specific

  • 0

I am trying to trim a string to the first occurrence of a specific word in a single string of comma separated words. E.g.:

deleteLastOccurrence("foo,bar,dog,cat,dog,bird","dog")

should return

"foo,bar,dog"

I have the following, and it doesn’t seem to be working correctly:

public String deleteLastOccurrence(String original, String target){
    String[] arr = original.split(",");
    arr = Arrays.copyOfRange(arr, Arrays.asList(arr).indexOf(target), original.length()-1);
    path = StringUtils.join(pathArray,",");
}

Any suggestions on a simpler method? Thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:42:53+00:00Added an answer on June 4, 2026 at 1:42 pm

    Use regex replace:

    public static String deleteLastOccurrence(String original, String target){
        return original.replaceAll("(,)?\\b" + target + "\\b.*", "$1" + target);
    }
    

    This code also works when the target is the first or last word in the original (hence the regex syntax \b which means “word boundary”)

    Also, rename your method to deleteAfterFirstOccurrence(), because your current name is misleading: The “last occurrence” is irrelevant to what you want.

    Here’s a little test:

    public static void main(String[] args) {
        // Test for target in middle:
        System.out.println(deleteLastOccurrence("foo,bar,dog,cat,dog,bird,dog", "dog"));
        // Test for target at start:
        System.out.println(deleteLastOccurrence("dog,bar,dog,cat,dog,bird,dog", "dog"));
        // Test for target at end:
        System.out.println(deleteLastOccurrence("foo,bar,cat,bird,dog", "dog"));
    }
    

    Output:

    foo,bar,dog
    dog
    foo,bar,cat,bird,dog
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a string that contains someones name as "Last, First" to
I am trying to only capitalize the first letter of the first word when
I just came across the interesting problem of trying to trim the leading zeroes
I am trying to write a function that will trim all the letters off
I'm trying to get data from file. The file first has three lines of
I am trying to use both mysqli_real_escape_string and trim together before making a MySQL
I am trying to compare a string from getline(file,line) with a std::string s=mmee :
I'm just starting with Scheme. I'm trying to use some procedures from String Library.
I am trying to sanitize a string so that it can be used to
I've been trying to highlight a search result in DataTable. First, I loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.