How do I split a string like (int,x)
into [(, int, , x, )]
Asked
Active
Viewed 220 times
0

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
-
3you 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 Answers
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
-
-
1@KhalidShah: Changed the code to Java and Java is my main language :) I got so used to python here as comparatively people using Python are more than Java now :) But more happy to post a code in my mother language :) – Pushpesh Kumar Rajwanshi Jan 16 '19 at 04:39
-
1
-
@Kartik: Thanks for upvote and exactly this is where regex helps :) – Pushpesh Kumar Rajwanshi Jan 16 '19 at 04:48