-1

Hi all I am hoping to strip a string in Java into a String and an integer for example:

"String1"

into:

String StringName = "String";
int StringInt = 1;

the Strings also may vary in length, is there an easy way to do this in Java, apologies if easy complete newbie.

4
  • 3
    any logic from your side ? Commented Feb 20, 2014 at 15:18
  • 1
    In this case regex woud be nice:) Commented Feb 20, 2014 at 15:19
  • 1
    I accidentally posted an answer without seeing your effort. :) Have you got anything in your mind as a solution? You have to show it... Commented Feb 20, 2014 at 15:21
  • To extract number from string, check this stackoverflow.com/questions/1903252/… Commented Feb 20, 2014 at 15:22

4 Answers 4

2

You can do this with a regular expression: (.*?)(\\d+)$ Let me explain this starting at the end. $ matches the end of the string (I am not considering multi line strings here). \d (in java code needs to be escaped as \\d) matches any digit, so the last capturing group matches the integer at the end of the string. The first group matches all characters in front of that. A non-greedy evaluation *? is required in the first group, otherwise it would just match the whole string.

As a whole working example:

Matcher m = Pattern.compile("(.*?)(\\d+)$").matcher(yourString);
if (m.matches()) {
    String stringPart = m.group(1);
    int intPart = Integer.parseInt(m.group(2));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Honestly I'm not sure. My post got downvoted as soon as I disagreed with Ashish in the comment thread of his post, and about a minute after someone downvoted his post the rest of them got a -1 as well.
1

If the number is always at the end of your string, write a method that iterates through the string starting from the end until you hit a non-integer character and then take the substring using the index you've just found. This will probably be faster than regex in most cases since iterating through the end of the string will be O(n) (where n=[num_integers]) + O(1) for the substring method.

Comments

1
String stringName[<maximum length>]
int stringInt;
for( int i = 0; i < originalString.length(); i++){
   if(isDigit(originalString[i]){
      stringInt = originalString[i];
   }
   else{
      stringName[i] = originalString[i];
   }
}

This should move all characters from your original string into stringName unless an integer value comes along which will be save in stringInt

Comments

-1

I would suggest to use the regular expression.

String value = "string12asd";
String intValue = value.replaceAll("[^0-9]", "");
String stringValue = value.replaceAll("[0-9]","");
System.out.println("int are: "+intValue+" \n string are: "+stringValue);

Look at http://www.regular-expressions.info/tutorial.html for more detail about regular expression.

16 Comments

For this question I think regex is a little overkill.
Why ? he did not mentioned where the integer or string is gonna come
Why does it matter where the integer and string come from? A regular expression will have to iterate through the entire string while a more direct approach will only have to iterate over the number at the end.
This solution is horrible! What about StringWith1DigitBeforeEnd123?
I got correct result for StringWith1DigitBeforeEnd123
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.