How to get a string between two characters?

I have a line,

String s = "test string (67)"; 

I want to get no 67, which is the line between (and).

Can someone tell me how to do this?

+56
java string
Sep 26 '12 at 5:23
source share
18 answers

There is probably a very neat RegExp, but I'm noob in this area, so instead ...

 String s = "test string (67)"; s = s.substring(s.indexOf("(") + 1); s = s.substring(0, s.indexOf(")")); System.out.println(s); 
+66
Sep 26 '12 at 5:27
source share

Try it like this:

 String s="test string(67)"; String requiredString = s.substring(s.indexOf("(") + 1, s.indexOf(")")); 

Method signature for substring:

 s.substring(int start, int end); 
+48
Aug 07 '13 at 6:42 on
source share

A very useful solution to this problem that does not require you to do indexOf is to use the Apache Commons libraries.

  StringUtils.substringBetween(s, "(", ")"); 

This method will even allow you to handle, even if there are several occurrences of the trailing string that will not be easy if you look for the closing string of indexOf.

You can download this library here: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4

+39
Jul 07 '16 at 6:28
source share

Using regex:

  String s = "test string (67)"; Pattern p = Pattern.compile("\\(.*?\\)"); Matcher m = p.matcher(s); if(m.find()) System.out.println(m.group().subSequence(1, m.group().length()-1)); 
+21
Sep 26
source share

Java supports regular expressions , but they are rather cumbersome if you really want to use them to extract matches. I think the easiest way to get the desired string in your example is to simply use regular expression support in the String class replaceAll :

 String x = "test string (67)".replaceAll(".*\\(|\\).*", ""); // x is now the String "67" 

It just removes everything that is included and includes the first ( and the same for ) and everything after that. It just leaves the material between the brackets.

However, the result of this is String . If you need an integer result then you need to do another conversion:

 int n = Integer.parseInt(x); // n is now the integer 67 
+15
Sep 26 '12 at 5:37
source share

In one line, I suggest:

 String input = "test string (67)"; input = input.subString(input.indexOf("(")+1, input.lastIndexOf(")")); System.out.println(input);` 
+9
Sep 15 '14 at 10:11
source share
 String s = "test string (67)"; int start = 0; // '(' position in string int end = 0; // ')' position in string for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == '(') // Looking for '(' position in string start = i; else if(s.charAt(i) == ')') // Looking for ')' position in string end = i; } String number = s.substring(start+1, end); // you take value between start and end 
+6
Sep 26 '12 at 5:30
source share

You can use the Apache StringUtils shared library for this.

 import org.apache.commons.lang3.StringUtils; ... String s = "test string (67)"; s = StringUtils.substringBetween(s, "(", ")"); .... 
+3
Apr 11 '16 at 19:04
source share
 String result = s.substring(s.indexOf("(") + 1, s.indexOf(")")); 
+3
Mar 02 '17 at 9:55 on
source share

Another way to use the split method

 public static void main(String[] args) { String s = "test string (67)"; String[] ss; ss= s.split("\\("); ss = ss[1].split("\\)"); System.out.println(ss[0]); } 
+2
Sep 26
source share

Use Pattern and Matcher

 public class Chk { public static void main(String[] args) { String s = "test string (67)"; ArrayList<String> arL = new ArrayList<String>(); ArrayList<String> inL = new ArrayList<String>(); Pattern pat = Pattern.compile("\\(\\w+\\)"); Matcher mat = pat.matcher(s); while (mat.find()) { arL.add(mat.group()); System.out.println(mat.group()); } for (String sx : arL) { Pattern p = Pattern.compile("(\\w+)"); Matcher m = p.matcher(sx); while (m.find()) { inL.add(m.group()); System.out.println(m.group()); } } System.out.println(inL); } } 
+2
Sep 26
source share

The least common way I've found to do this is with the Regex and Pattern / Matcher classes:

 String text = "test string (67)"; String START = "\\("; // A literal "(" character in regex String END = "\\)"; // A literal ")" character in regex // Captures the word(s) between the above two character(s) String pattern = START + "(\w+)" + END; Pattern pattern = Pattern.compile(pattern); Matcher matcher = pattern.matcher(text); while(matcher.find()) { System.out.println(matcher.group() .replace(START, "").replace(END, "")); } 

This can help for more complex regex issues where you want to get text between two character sets.

+2
02 Feb '17 at 19:25
source share
 String s = "test string (67)"; System.out.println(s.substring(s.indexOf("(")+1,s.indexOf(")"))); 
+1
Feb 18 '16 at 11:50
source share

Another possible solution is to use lastIndexOf , where it will look for a character or string from the return.

In my script, I had the following String and I had to extract <<UserName>>

 1QAJK-WKJSH_MyApplication_Extract_<<UserName>>.arc 

So, indexOf and StringUtils.substringBetween did not help, since they started looking for the character from the beginning.

So I used lastIndexOf

 String str = "1QAJK-WKJSH_MyApplication_Extract_<<UserName>>.arc"; String userName = str.substring(str.lastIndexOf("_") + 1, str.lastIndexOf(".")); 

And it gives me

 <<UserName>> 
+1
Oct 05 '17 at 18:49
source share
 public String getStringBetweenTwoChars(String input, String startChar, String endChar) { try { int start = input.indexOf(startChar); if (start != -1) { int end = input.indexOf(endChar, start + startChar.length()); if (end != -1) { return input.substring(start + startChar.length(), end); } } } catch (Exception e) { e.printStackTrace(); } return input; // return null; || return "" ; } 

Using:

 String s = "test string (67)"; String output = getStringBetweenTwoChars(s, "(", ")"); System.out.println(output); // Output: "67" 
+1
Oct 27 '18 at 17:21
source share

A β€œcommon” way to do this is to parse the line from the very beginning, discarding all characters before the first bracket, write the characters after the first bracket, and discard the characters after the second bracket.

I am sure there is a regular expression library or something like that.

0
Sep 26 '12 at 5:26
source share

Something like that:

 public static String innerSubString(String txt, char prefix, char suffix) { if(txt != null && txt.length() > 1) { int start = 0, end = 0; char token; for(int i = 0; i < txt.length(); i++) { token = txt.charAt(i); if(token == prefix) start = i; else if(token == suffix) end = i; } if(start + 1 < end) return txt.substring(start+1, end); } return null; } 
0
Jun 30 '18 at 0:55
source share

I got such an answer. Try

 String value = "test string (67)"; int intValue =Integer.valueOf( value.replaceAll("[^0-9]", "")); 
-one
Sep 05 '14 at 5:51 on
source share



All Articles