
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we add an underscore before each capital letter inside a java String?
Using the StringBuffer class
To add underscore before each capital letter in a String using StringBuffer −
Create an empty StringBuffer object.
The isUpperCase() method of Character class accepts a character and verifies whether it is in upper case, if so, this method returns true. Using this method, verify each character in the String.
In case of upper case letter append underscore before it, using the append() method.
Example
public class Adding_BeforeCapital { public static void main(String args[]) { String str = "HelloHowAreYouWelcome"; StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { if(Character.isUpperCase(str.charAt(i))) { sb.append("_"); sb.append(str.charAt(i)); } else { sb.append(str.charAt(i)); } } String result = sb.toString(); System.out.println(result); } }
Output
_Hello_How_Are_You_Welcome
Using regular expression
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.
Example
public class Adding_BeforeCapital { public static void main(String args[]) { String str = "HelloHowAreYouWelcome"; String result = str.replaceAll("()([A-Z])", "$1_$2"); System.out.println(result); } }
Output
_Hello_How_Are_You_Welcome
- Related Articles
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How can I test if a string starts with a capital letter using Python?
- Can we define an interface inside a Java class?
- How can we print all the capital letters of a given string in Java?
- How to add a string before each numeric value in an R data frame column?
- Can we define an enum inside a class in Java?
- Can we define an enum inside a method in Java?
- Can we define constructor inside an interface in java?
- Can we define a class inside a Java interface?
- Can we give underscore in a MySQL table name?
- In how many ways can we find a substring inside a string in javascript?
- How can we display the line numbers inside a JTextArea in Java?
- How can we disable the cell editing inside a JTable in Java?
- How can we add a JSONArray within JSONObject in Java?
- How can we add padding to a JTextField in Java?

Advertisements