Skip to content

Commit 0e1bf3a

Browse files
committed
text between delimiters extraction snippet
1 parent dee9622 commit 0e1bf3a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Extract Text Between Delimiters
3+
description: Extracts a text between two given delimiters from a string
4+
author: Mcbencrafter
5+
tags: string,delimiters,start,end
6+
---
7+
8+
```java
9+
public static String extractBetweenDelimiters(String text, String start, String end) {
10+
int startIndex = text.indexOf(start);
11+
int endIndex = text.indexOf(end, startIndex + start.length());
12+
13+
if (startIndex == -1 || endIndex == -1)
14+
return "";
15+
16+
return text.substring(startIndex + start.length(), endIndex);
17+
}
18+
19+
// Usage:
20+
System.out.println(extractBetweenDelimiters("hello, world!", ",", "!")); // " world"
21+
```

0 commit comments

Comments
 (0)