Learn various ways to locate the n-th occurrence of a substring within a string using iterative, recursive, and regex-based solutions.
Also find me here:
Baeldung Author
Kai Yuan
Kai Yuan has been developing software for more than 20 years, mainly working on enterprise Kotlin and Java applications. He’s been using Linux for over 20 years and is a big fan of the Vim editor. Kai also enjoys helping out on StackOverflow and currently lives in Hamburg.
Here's what I've written (so far):
Baeldung on Java
- All
- Java String (42)
- Java List (25)
- Java Array (18)
- Core Java (18)
- Java Numbers (16)
- Java Map (16)
- Java IO (14)
- Java (11)
- Algorithms (10)
- Java Streams (8)
- Testing (7)
- Java Collections (5)
- Persistence (4)
- Maven (4)
- Spring Boot (2)
- Java Concurrency (2)
- XML (1)
- Spring Persistence (1)
- Networking (1)
- Java Web (1)
- Jakarta EE (1)
- Jackson (1)
- IDE (1)
How to Get First or Last Entry From a LinkedHashMap in Java
Filed under Java Map
Learn various methods for retrieving the first and last key-value pairs from a LinkedHashMap
Difference Between putIfAbsent() and computeIfAbsent() in Java’s Map
Filed under Java Map
Learn the differences between Java Map’s putIfAbsent() and computeIfAbsent() through examples.
Joining a List<String> in Java With Commas and “and”
Filed under Java List
Explore an interesting string concatenation problem: joining strings in a more natural language-like manner.
Get All Record Fields and Its Values via Reflection
Filed under Core Java
Explore how to retrieve all the fields and their values within a record class using Java’s Reflection API.
How to Remove Line Breaks From a File in Java
Filed under Java IO
Learn about the different kinds of line breaks and explore various approaches to removing line breaks from a file.
Check if a List Contains a String Element While Ignoring Case
Filed under Java List
Explore two approaches to performing a case-insensitive check to determine if a string list contains a specific string.
Assert Regex Matches in JUnit
Filed under Testing
Explore several approaches to asserting regex matches in JUnit, empowering us to test our string patterns effectively.
How to Split an Integer Number Into Digits in Java
Filed under Java Numbers
Explore various approaches to splitting an integer number into its constituent digits using Java.
Baeldung on Linux
- All
- Scripting (36)
- Files (32)
- File Editing (21)
- Text Processing (11)
- File Searching (11)
- Processes (10)
- Administration (10)
- Filesystems (6)
- File Viewing (6)
- User Administration (4)
- Search (3)
- File Permissions (3)
- Web (2)
- Security (1)
- File Compression (1)
- Boot Process (1)
How to Limit grep Context to “n” Characters in a Line
Filed under File Searching
Learn how to limit the grep output characters displayed before and after the match.
Guide to Linux dirname Command with Examples
Learn how to use the dirname command in Linux to extract the directory part of a given path.
Guide to Linux basename Command with Examples
Learn about how to use the basename command to determine the file or folder name for one or more paths.
Printing Quote Characters in AWK
Filed under Text Processing
Learn how to print single or double quote characters in AWK scripts.
How to Read the First and the Last Line From Input
Filed under File Viewing
Learn how to extract the first and last lines from an input file using a single command.
Printing Specified Lines From a File
Filed under File Viewing
Learn how to extract specific lines from a file by line numbers using sed and awk.
Replace the First n Matched Instances in a File
Filed under File Editing
Learn how to replace the first n pattern occurrences in a file using sed and awk.
How to Operate on Files Listed in a File
Learn to use various techniques to operate on files listed in a file.
What’s the Meaning of $! in Bash Scripting
Learn the meaning of the special shell variable $!.
Baeldung on Kotlin
- All
- Kotlin Collections (18)
- Kotlin Classes and Objects (10)
- Kotlin Basics (10)
- Kotlin Numbers (9)
- Kotlin Strings (7)
- Kotlin Functions (5)
- Kotlin (4)
- Kotlin Arrays (3)
- Testing (2)
- Functional Programming (2)
- Spring (1)
- Security (1)
- Libraries (1)
- Algorithms (1)
Resolving Compile Error: “Const ‘val’ is only allowed on top level, in named objects, or in companion objects” in Kotlin
Filed under Kotlin Classes and Objects
Learn where and how you can use const val in Kotlin.
Resolving “Type Mismatch Inferred Type is Unit But Void Was Expected” in Kotlin
Filed under Kotlin Functions
Learn how to solve the “Unit but Void was expected” compilation error in Kotlin.
Iterating Enum Entries in Kotlin
Filed under Kotlin Basics
Learn how to loop over the entries of an Enum in Kotlin.
Difference Between clearAllMocks() and unmockkAll() in MockK
Filed under Testing
Learn about the difference between clearAllMocks() and unmockkAll() in MockK and when to use each one.
Serialize Enum Property to JSON in Kotlin
Filed under Libraries
Learn how to serialize a Kotlin enum property to JSON using Kotlin libraries.
Finding First Match in Nested Lists in Kotlin
Filed under Algorithms, Kotlin Collections
Learn how to search through a list of lists in Kotlin to find the first matching element.
Conditional Exception Throwing in Kotlin
Filed under Kotlin
Learn different approaches to throwing an exception when a condition is true in Kotlin.
Pass a Function as Parameter to Another in Kotlin
Filed under Kotlin Functions
Learn about how functions can be passed as parameters to other functions in Kotlin.
Matching Varargs Using MockK in Kotlin
Filed under Testing
Learn how to use the MockK mocking library to match varargs arguments in Kotlin.
Baeldung on Ops
- All
- Git (5)
Remove File From Git Repository Without Deleting It Locally
Filed under Git
Learn how to remove a file or directory from a Git repository but keep its local copy
How to Get the Current Branch Name in Git
Filed under Git
Learn how to get the Git branch name we’re currently working on
Delete a Git Branch Locally and Remotely
Filed under Git
Learn how to delete Git branches
Move Existing, Uncommitted Work to a New Branch in Git
Filed under Git
Learn a couple of quick ways to move uncommitted changes to a new Git branch
Squash the Last X Commits Using Git
Filed under Git
Learn what Git squashing is, talk about when we need to squash commits, and take a closer look at how to squash commits