
- 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
Moving all Upper case letters to the end of the String using Java RegEx
The subexpression “[ ]” matches all the characters specified in the braces. Therefore, to move all the upper case letters to the end of a string −
Iterate through all the characters in the given string.
Match all the upper case letters in the given string using the regular expression "[A-Z]".
Concatenate the special characters and the remaining characters to two different strings.
Finally, concatenate the special characters string to the other string.
Example 1
public class RemovingSpecialCharacters { public static void main(String args[]) { String input = "sample B text C with G upper case LM characters in between"; String regex = "[A-Z]"; String specialChars = ""; String inputData = ""; for(int i=0; i< input.length(); i++) { char ch = input.charAt(i); if(String.valueOf(ch).matches(regex)) { specialChars = specialChars + ch; } else { inputData = inputData + ch; } } System.out.println("Result: "+inputData+specialChars); } }
Output
Result: sample text with upper case characters in betweenBCGLM
Example 2
Following is the Java program which moves the upper case letters of a string to its end using the methods of Regex package.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String args[]) { String input = "sample B text C with G upper case LM characters in between"; String regex = "[A-Z]"; String specialChars = ""; System.out.println("Input string: \n"+input); //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); //Creating an empty string buffer StringBuffer sb = new StringBuffer(); while (matcher.find()) { specialChars = specialChars+matcher.group(); matcher.appendReplacement(sb, ""); } matcher.appendTail(sb); System.out.println("Result: \n"+ sb.toString()+specialChars ); } }
Output
Input string: sample B text C with G upper case LM characters in between Result: sample text with upper case characters in betweenBCGLM
- Related Articles
- Moving all special char to the end of the String using Java Regular Expression RegEx)
- Moving all vowels to the end of string using JavaScript
- Python regex to find sequences of one upper case letter followed by lower case letters
- How match a string irrespective of case using Java regex.
- How to match end of a particular string/line using Java RegEx
- How to match end of the input using Java RegEx?
- How to check if a string contains only upper case letters in Python?
- How to match a character from given string including case using Java regex?
- How to transform List string to upper case in Java?
- PHP – Make an upper case string using mb_strtoupper()
- How to convert a string into upper case using JavaScript?
- How to invert case for all letters in a string in Python?
- Convert a C++ String to Upper Case
- Converting to upper case in Java.
- How to generate random strings with upper case letters and digits in Python?

Advertisements