I want to get two integer values separated form a string XmodX
one is before mod and other is after mod the integers can be of any length. what is the best way to to so?
Asked
Active
Viewed 63 times
-5

Billal Begueradj
- 20,717
- 43
- 112
- 130

Salman Arshad
- 343
- 6
- 23
-
1Did you try anything? – Oliver Charlesworth Nov 12 '17 at 14:25
-
2Possible duplicate of [String delimiter in string.split method](https://stackoverflow.com/questions/7021074/string-delimiter-in-string-split-method) – aaDev Nov 12 '17 at 14:26
-
2Questions asking for *homework help* **must** include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it ([help], [ask]). – Zabuzard Nov 12 '17 at 14:31
-
**split** on `mod` and then parse. – Zabuzard Nov 12 '17 at 14:31
1 Answers
1
This approach should work...
// The variable 'parts' will contain 2 items: your 2 integers, though they will still be String objects
String[] parts = myString.split("mod");
try {
int firstInt = Integer.parseInt(parts[0]);
int secondInt = Integer.parseInt(parts[1]);
) catch(NumberFormatException nfe) {
// One of your Strings was not an integer value
}

Drew Wills
- 8,408
- 4
- 29
- 40