For 1), here's a very straightforward regex way:

  String in = "helllo goodbye";
  String out = in.replaceAll("(.)(?=\\1)", "$1*");
  System.out.println(out);

Prints:

hel*l*lo go*odbye

Explanation:

(.)     //match any one character into group 1
(?=\\1) //positive lookahead for that same character by backreferencing group 1

$1*     //replace that one character with the character followed by *

I might take a crack at 2) later, but I don't like multiple questions wrapped into one.

Edit

Alright since I'm in a regex mood here's 2):

  String in = "xhixhix";
  String out = in;
  while (!out.matches("[^x]*x*")) {
     out = out.replaceAll("x(.*)", "$1x");
  }
  System.out.println(out);

This replaces x(something) with (something)x until all the xs are at the end of the string. I'm sure that there's a better way of doing this one with/than regex though.

Answer from Mark Peters on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-programs
Java String Programs - GeeksforGeeks
October 7, 2025 - Strings are widely used for storing and processing textual data in programs. Here is a complete list of Java String practice programs divided into basic and advanced categories:
🌐
Programiz
programiz.com › java-programming › string
Java String (With Examples)
And all string variables are instances of the String class. Java provides various string methods to perform different operations on strings.
Discussions

String Manipulation Problems in Java - Stack Overflow
We know what you meant, but if ... get a simple introduction to programming (or Java). 2011-03-28T15:04:00.977Z+00:00 ... Maybe I have misunderstood the question but if not if seems pretty simple. Your method could be something like for number 1: public String pairStar(String ... More on stackoverflow.com
🌐 stackoverflow.com
bufferedreader - Java String Program - Stack Overflow
Explore Stack Internal ... Save this question. Show activity on this post. I'm trying to do a String Program using BufferedReader where you take a String from the user and change the case of the letters. this is what I've got so far: Copyimport java.io.*; public class StringProg { public void ... More on stackoverflow.com
🌐 stackoverflow.com
manipulating strings in Java
There are indeed usually many ways to achieve the same result in programming. But some ways better than the other and it takes time and experience to figure which. When it comes to strings, there are several things to keep in mind. Strings are immutable. Don't use new String(xxx) constructor, it was a design mistake. If you need to do a lot of modifications to a string, especially in a loop, it's advised to use StringBuilder, it works faster. Be careful when there can be non-English characters or Unicode emotes in the string. In these cases charAt and length might return not what you think. Most of the time however we just pass strings around, so these things don't come up too often. More on reddit.com
🌐 r/learnjava
6
2
February 20, 2024
What Every Java Developer should know about String
[String.split() is] particularly useful if you dealing with comma separated file (CSV) and wanted to have individual part in a String array. No. Fuck you to hell if you parse CSV this way. It will break, and it will all be your fault. CSV allows for commas to be inside the records as long as they're quoted (or escaped), so a line in a CSV file could look like this: "There is only one record on this line. Because it has commas inside quotes, it is one single, unbroken record" String.split() will parse that line wrong. More on reddit.com
🌐 r/java
43
49
January 7, 2014
🌐
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › 25+ frequently asked java string interview programs
25+ Frequently Asked Java String Interview Programs
March 22, 2022 - Palindrome programs are one of the most asked Java programming interview questions for freshers. In this article, you will learn to write palindrome program using 4 different methods. They are iterative method, recursive method, Palindrome program using StringBuffer and palindrome program using IntStream of Java 8.
🌐
Smartprogramming
smartprogramming.in › tutorials › java › string-programs
Java String Programs | String Logical Questions
26 WAP to determine the Unicode code point at a given index in a String · Previous Topic · Next Topic · Your feedback helps us grow! If there's anything we can fix or improve, please let us know. We’re here to make our tutorials better based on your thoughts and suggestions. Provide Feedback · Get 80% off on our Industry Level Courses · Home · Free Courses · Premium Courses · Trainings · Development · Contact Us · Core Java Free Course ·
🌐
Medium
medium.com › @suryapriyamusuwathi › basic-java-programs-in-strings-can-be-a-great-starting-point-for-learning-to-code-part-3-95b00b5eb5ef
Basic Java programs in Strings can be a great starting point for learning to code Part-3 | by Suryapriyamusuwathi | Medium
July 24, 2024 - /** * 1. String class split method * 2. Pattern.compile(regex).splitAsStream(String) * 3. String Tokenizer * 4. StringUtils.split(string,"expression"); */ // Method 1 String str = "091-879456789"; String strArr[] = str.split("-"); for(String s:strArr) { System.out.println("Method 1:"+s); } //Method 2: Pattern.compile List<String> strings = Pattern.compile("-").splitAsStream(str).collect(Collectors.toList()); for(String string:strings) { System.out.println("Method 2: "+string); } //Method 3 //StringTokenizer method StringTokenizer st1 = new StringTokenizer( str, "-"); while(st1.hasMoreTokens()) { System.out.println(st1.nextToken()); } } }
Find elsewhere
🌐
GitHub
github.com › imavipatel › String-Programs
GitHub - imavipatel/String-Programs: All Programs related to String In Java · GitHub
All Programs related to String In Java. Contribute to imavipatel/String-Programs development by creating an account on GitHub.
Author   imavipatel
🌐
Scientech Easy
scientecheasy.com › home › blog
Top 15+ String Programs in Java for Interview - Scientech Easy
June 11, 2025 - Output: Number of words in a string = 4 Number of words in a string = 2 Number of words in a string = 5 · [blocksy-content-block id=”12153″] Question 4: Let’s write a Java program to determine whether one string is the rotation of another string.
🌐
InterviewBit
interviewbit.com › java-string-interview-questions
Top Java String Interview Questions & Answers (2025) - InterviewBit
December 21, 2024 - Strings, one of the most common objects used in Java programming, are essentially sequences of characters. As an example, the string "Scaler" contains the following characters: "S", "c", "a", "l", "e", and "r". You can either create a string ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java String Programs - Programming Examples for Interviews (2021) - Java Code Geeks
March 3, 2021 - In this article, we have seen the most used java string programs with examples.
🌐
Crio
crio.do › blog › string-methods-in-java
10 Important String Methods In Java You Must Know
October 20, 2022 - Useful and important methods in Java. indexOf, toCharArray, charAt, concat, replace, substring, split, compareTo, strip, valueOf
🌐
CodingBat
codingbat.com › java › String-1
CodingBat Java String-1
Basic string problems -- no loops. Use + to combine Strings, str.length() is the number of chars in a String, str.substring(i, j) extracts the substring starting at index i and running up to but not including index j. New videos: String Introduction, String Substring · Java Example Solution Code ·
🌐
DigitalOcean
digitalocean.com › community › tutorials › string-programs-in-java
String Programs in Java | DigitalOcean
August 4, 2022 - The most important point to understand is how Strings get created in java. When we create a String using string literal, it doesn’t change the value of original String. It creates a new String in the string pool and change the reference of the variable. So original string value is never changed and that’s why Strings are immutable. Below program proofs our statement, read out the comments for proper understanding the concept.
🌐
Reddit
reddit.com › r/learnjava › manipulating strings in java
r/learnjava on Reddit: manipulating strings in Java
February 20, 2024 -

So, I'm currently in week 5 of my intro to programming class, and I just noticed that when it comes to manipulating Strings, chars, or just any text in general, it's very difficult for me. I'm not having any issues figuring out loops and what not when it comes to numbers, but strings have been very very difficult for me. I was just working on a problem trying to create a for loop that took the users imputed word and added the same word onto itself x number of times, x being the amount of characters in the users word. I've figured it out with the help of a tutor, but he showed me how to do it my way and also showed me how he would have done it. They were two different ways of setting up the for loop which I get there's a million different ways to do stuff in Java, I'm just having a very hard time manipulating text and haven't had any issues manipulating numbers. Has anyone else been through this same situation? Maybe I'm making it harder on myself? I also know lots of practice is the key to coding so I'm doing all the practicing I can. Just looking for opinions on what I should focus on or maybe a thought process that would help? Thanks.

Edit: Also, are there any specific things I should study or career paths that I should research that mainly deal with numbers when coding?

🌐
BeginnersBook
beginnersbook.com › 2017 › 09 › java-examples
Java Programs – Java Programming Examples with Output
Java Program to sort strings in alphabetical order · Java Program to reverse words in a String · Java Program to perform bubble sort on Strings · Java program to find occurrence of a character in a String · Java program to count vowels and consonants in a String ·
🌐
Edureka
edureka.co › blog › java-string
Java Strings - How to Declare String in Java With Examples
February 13, 2025 - This program shows the comparison between the various string. It is noticed that if s1 > s2, it returns a positive number if s1 < s2, it returns a negative number if s1 == s2, it returns 0 · Java String concat() : This method combines a specific string at the end of another string and ultimately returns a combined string.
🌐
Refreshjava
refreshjava.com › java › array-and-string-programs
Array and String Programs in Java - RefreshJava
Let's write one more program which counts total number of occurrence of a given character inside a given string. The program shows two different approaches to count the total occurrence. class CharacterCount { public static void main(String[] args) { String str = "Java is easy to learn"; char c = 'a'; int count = 0; // First Approach char[] charArray = str.toCharArray(); for(char ch : charArray) { if(ch == c) count++; } System.out.println("Total occurrence of character 'a' using 1st approach = "+count); // Second Approach int count2 = str.length() - str.replace("a", "").length(); System.out.println("Total occurrence of character 'a' using 2nd approach = "+count2); } }
🌐
Sanfoundry
sanfoundry.com › java-programming-examples-set-string-problems-algorithms
String Programs in Java - Sanfoundry
August 7, 2023 - In Java programming, a string is an object that represents a sequence of characters. Strings are created and manipulated using the String class. A string can be created in Java using either a string literal or a new keyword.