0

How do I split a string like (int,x) into [(, int, , x, )]

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
mastercool
  • 463
  • 12
  • 35
  • You mean like `str.split("");`? This will split on every character – GBlodgett Jan 16 '19 at 04:21
  • 3
    you need to add more explanation to your question & illustrate by an example by taking a sample input & expected output. – Sabir Khan Jan 16 '19 at 04:22
  • You are confused in asking what you wanna ask :) – Vishwa Ratna Jan 16 '19 at 04:28
  • And what have you tried so far? – Nicholas K Jan 16 '19 at 06:07
  • Please show your attempt at solving the problem, define what you mean exactly by special characters (is `_` also a special character for example) and tell what should happen if there are multiple of these special characters next to each other, e.g. `(int,,x)` – Jerry Jan 16 '19 at 07:50

1 Answers1

8

With your expected output, you can use \b to split your string and get the expected output,

Here is the Java code,

String s = "(int,x)";
System.out.println(Arrays.toString(s.split("\\b")));

Prints,

[(, int, ,, x, )]
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36