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 7511443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:32:52+00:00 2026-05-29T23:32:52+00:00

I have written a function in my program that allows me to retrieve strings

  • 0

I have written a function in my program that allows me to retrieve strings and a separate string. ie, the string:

‘copy “C:\Users\USERNAME\Desktop\file.bat” “C:\Users\”‘

would result in having a string like: ‘C:\Users\USERNAME\Desktop\file.bat’ with the function getArgs(command, 0), and the other ‘C:\Users\’ with the function getArgs(command, 1).

The problem is that the function always seems to retrieve an empty string. Please be lenient on me, this is my first time using string manipulation functions in Java.

Notice: When I say empty I do not mean NULL, I mean “”.

static String getArgs(String command, int argumentIndex)  {
    int start = 0;
    int end = 0;

    for (int i = 0; i <= argumentIndex; i++)  {
        start = command.indexOf("\"", end);
        end = command.indexOf("\"", start);

        if (i == argumentIndex)  {
            return command.substring(start, end);    
        }
    }

    return null;
}

Any ideas? Thanks.

  • 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-05-29T23:32:53+00:00Added an answer on May 29, 2026 at 11:32 pm

    Try the following correction. Keep in mind that your solution while work only if all the arguments in the command string are wrapped in quotes, even the ones without spaces. In the example you provided the first argument (‘copy’) must be also wrapped in quotes since you are using the quotes as delimiters.

    static String getArgs(String command, int argumentIndex)  {
        int start = 0;
        int end = -1;
    
        for (int i = 0; i <= argumentIndex; i++)  {
            start = command.indexOf("\"", end+1)+1;
            end = command.indexOf("\"", start+1);
    
            if (i == argumentIndex)  {
                return command.substring(start, end);    
            }
        }
    
        return null;
     }
    

    Try this for a more generic solution which accepts arguments without quotes also

    static String getArgs(String command, int argumentIndex)  {
        int start = 0;
        int end = -1;
        String delimiter = " ";
        for (int i = 0; i <= argumentIndex && !command.equals(""); i++)  {
            if (command.startsWith("\"")) {
                delimiter = "\"";
                start = 1;
            } else {
                delimiter = " ";
                start = 0;
            }
    
            end = command.indexOf(delimiter, start+1);
            if (i == argumentIndex)  {
                end = (end==-1?command.length():end);
                return command.substring(start, end).trim();    
            } else {
                end = (end==-1?command.length():end+1);
                command = command.substring(end).trim();
            }
        }
        return null;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a javascript function that uses setInterval to manipulate a string every
I have written a program that relies on a proxy to function. I now
I have written a function that gets a given number of random records from
I have written a function that positions a tooltip just above a textbox. The
I have written a PHP function to take a video embed code that has
I have written a program that in the client section an error occures frequently,
Below is a program i have written that ran fine when I type what
i have written a snake program using javascript.. the problem is that the snake
I have written a program that runs on Linux and uses sigevent and timer_create
I have written a php program that generates rss feeds, however I am having

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.