How do I find a number in a random string ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I find a number in a random string ?

10th Jan 2020, 5:30 PM
Jak
11 Answers
+ 8
just run this code i hope this will help you import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("Input : "+s); Pattern p=Pattern.compile("\\d"); Matcher m=p.matcher(s); while(m.find()) { System.out.println("The digit : "+m.group()+" Located in "+m.start()+" th index") ; } } }
12th Jan 2020, 5:35 AM
Saroj Patel
Saroj Patel - avatar
+ 6
What programming language you are talking about? Can you provide us a sample? It would also be great if you show us your attampt by linking it from Playground here.
10th Jan 2020, 5:39 PM
Lothar
Lothar - avatar
+ 4
In python if you want to use regular expressions, there is the re.findall() function. Try to think about the problem in more detail. A random string may contain multiple digits and may be letters inbetween Example "a1b23c" What number do you want to capture? 1? 1 and 23? 1 and 2 and 3? 123?
10th Jan 2020, 5:56 PM
Tibor Santa
Tibor Santa - avatar
+ 4
/\d+/ - for integers /-?\d+\.?\d*/ - for any signed integers and decimal point number
10th Jan 2020, 9:47 PM
Anuran Pal
Anuran Pal - avatar
+ 4
You can use CharMatcher in java CharMatcher.digit().retainFrom("1+2=3"); //123
11th Jan 2020, 2:46 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
Jak Look here: https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_regex.asp https://docs.python.org/3/library/re.html import re test = "hello123#/^afgee567d" list = re.findall("[0-9]", test) print(list)
10th Jan 2020, 5:59 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Its python. Thanks for all who answered
10th Jan 2020, 6:06 PM
Jak
+ 2
try converting a string character by character into an array. where each cell will have one character. then in a loop, iterate through each character and look for numbers
10th Jan 2020, 5:44 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Use.. String.isdigit() This will give you 'True' if it is a digit.
12th Jan 2020, 7:30 AM
Ashutosh R. Yadav
Ashutosh R. Yadav - avatar
+ 1
Jak May I know in which language you're talking about?
10th Jan 2020, 5:54 PM
Sarthak
Sarthak - avatar
+ 1
You can use === operator in if statement along with for or while loop.
12th Jan 2020, 2:12 PM
Abhishek nirwan
Abhishek nirwan - avatar