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

  • SEARCH
  • Home
  • 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 7645959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:04:59+00:00 2026-05-31T10:04:59+00:00

I created this method deleteCharAt in order to remove a char from a string

  • 0

I created this method deleteCharAt in order to remove a char from a string by its index:

public String deleteCharAt(String str, int i) {

    if (i == 0) {
        return str.substring(1);
    }
    else if (i == str.length()) {
        return str.substring(0, i-1);
    }

    String first = str.substring(0, i - 1);
    String second = str.substring(i+1);

    return first + second;
}

However it’s not working as expected, I think it may be because I’m not understanding how the substring function works.

Does this look correct? Will this code remove the i-th character from a string successfully? Or did I mess up the substring function?

  • 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-31T10:05:00+00:00Added an answer on May 31, 2026 at 10:05 am

    It’s not quite right – this:

    String first = str.substring(0, i - 1);
    

    should be:

    String first = str.substring(0, i);
    

    (Think about a simple example – if i is 1, you want to take substring(0, 1) to get the first character; substring(0, 0) would give an empty string.)

    because the second parameter of substring is already exclusive.

    Likewise this optimization:

    else if (i == str.length()) {
        return str.substring(0, i);
    }
    

    should be:

    else if (i == str.length() - 1) {
        return str.substring(0, i);
    }
    

    You should also add argument validation.

    After making those changes, this code:

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(deleteCharAt("0123456789", i));
        }
    }
    

    Gives this output:

    123456789
    023456789
    013456789
    012456789
    012356789
    012346789
    012345789
    012345689
    012345679
    012345678
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a method where I can search for string placeholders, this I
I've created this extension method public static void AddIfNullCreate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey
I have created this method which is an object factory: public static T GetService<T>(T
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I've created this utility method in JS: function IsAuthenticated(userID) { var isAuthed = false;
Example: // socket created above this int iSocketOption = 1; setsockopt(CFSocketGetNative(socket), SOL_SOCKET, SO_REUSEADDR, (void
I've created this method to change every value in an array. I'm kinda new
Why does this init method return an object out of scope? Using XCode 4.2,
I just wrote a delegate method, that shall return an array, I've created inside
hi friend i created this method for image select it working proper but i

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.