+ 1
How do I find a number in a random string ?
12 Respostas
+ 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") ;
}
}
}
+ 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.
+ 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?
+ 4
/\d+/ - for integers
/-?\d+\.?\d*/ - for any signed integers and decimal point number
+ 4
You can use CharMatcher in java
CharMatcher.digit().retainFrom("1+2=3");
//123
+ 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)
+ 3
Its python. Thanks for all who answered
+ 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
+ 2
Use..
String.isdigit()
This will give you 'True' if it is a digit.
+ 1
Jak May I know in which language you're talking about?
+ 1
You can use === operator in if statement along with for or while loop.