- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to remove a character at a specified position
To remove a character at a specified position, use the following logic −
Let’s say the following is our string.
String str = "The Haunting of Hill House!";
We want to remove a character at position 7. For that, use the below logic.
// removing character at position 7 int pos = 7; String res = str.substring(0, pos) + str.substring(pos + 1);
The following is the complete example with output.
Example
public class Demo { public static void main(String[] args) { String str = "The Haunting of Hill House!"; System.out.println("String: "+str); // removing character at position 7 int pos = 7; String res = str.substring(0, pos) + str.substring(pos + 1); System.out.println("String after removing a character: "+res); } }
Output
String: The Haunting of Hill House! String after removing a character: The Hauting of Hill House!
Advertisements