17

I want to extract the number from an alphanumeric string. This is what I tried:

StringTake["thiru3", {6, 6}]

The result I got is 3, but it is still a String, which I determined by evaluating:

NumberQ[StringTake["thiru3", {6, 6}]]

which returns False.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
thirupathi
  • 193
  • 1
  • 1
  • 4

9 Answers9

19
 StringCases["thiru3", x : NumberString :> ToExpression[x]]
 (* {3} *)
 First[%]
 (* 3 *)
 {NumberQ[%], Head[%]}
 (* {True, Integer} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
  • 1
    As noted by ssch in Chris's answer, FromDigits[] might be better to use than ToExpression[]. – J. M.'s missing motivation Apr 04 '13 at 14:55
  • 1
    NumberString will match real number strings and FromDigits will balk; ToExpression will convert them. I would say NumberString & ToExpression for real numbers, or, for integers only, DigitCharacter.. and FromDigits. You won't get messages in either case, as in ssch's comment, because FromDigits is called only when there is a match. – Michael E2 Oct 16 '13 at 12:09
12

Try to use ToExpression, like this

ToExpression[StringTake["thiru3", {6, 6}]]

If you check it with Head[%] it confirms that it is an Integer

Crown42
  • 463
  • 2
  • 8
11

Alternatively, and for multiple numbers:-

Map[FromDigits, Select[Characters["thiru37"], DigitQ]]

{3, 7}

Latterly

ToExpression@StringCases["thiru37", DigitCharacter]

{3, 7}

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • 2
    I think FromDigits is the best way since you will immediatly get a message printed in case it didn't work, that you can Check for if need be – ssch Dec 22 '12 at 19:23
9

Here is one compact option

Internal`StringToDouble@"thiru3"

3.

Murta
  • 26,275
  • 6
  • 76
  • 166
3

Like NumberString, DigitCharacter can be used to find numbers in a string. Use relative positioning in the string (like EndOfString) to localize your digits and ignore other numbers possibly appearing at other positions:

First@StringCases["0thi12ru3", (n:DigitCharacter~~EndOfString) :> ToExpression@n]

(* ==> 3 *)

This searches for exactly one number character right before the end of string. If the terminal number could have more than one digits, use DigitCharacter.. instead.

István Zachar
  • 47,032
  • 20
  • 143
  • 291
3

Consider:

str = "thiru37aa2er45"

then

StringCases[str, DigitCharacter ..]

yields:

{"37", "2", "45"}

StringCases[str, DigitCharacter .]

yields:

{"3", "7", "2", "4", "5"}

They can be converted to expressions using ToExpression

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
2
s = "thiru3";
r = First@StringCases[s, RegularExpression["\\d+"]];
Needs["JLink`"];
InstallJava[];
LoadJavaClass["java.lang.Integer"];
z = Integer`parseInt[r]
Head[z]

Mathematica graphics

Nasser
  • 143,286
  • 11
  • 154
  • 359
1

Can be so

ToExpression[StringCases["thiru3", DigitCharacter ..]][[1]]

or

ToExpression[
  StringCases["thiru3", "thiru" ~~ (x : DigitCharacter ..) -> x]][[1]]

or

ToExpression[StringCases["thiru3", RegularExpression["\\d+"]][[1]]]
0

Try this.

Read[StringToStream[StringTake["thiru3", {6, 6}]]]
subbu
  • 2,304
  • 1
  • 13
  • 32