fetch

How to Extract digits from String in Java | Programming for Selenium | Webdriver Interview Questions



Sharing buttons:

hey hi everyone today we'll talk about

another selenium interview question

which is how to extract the digits from

a string yes sometimes there are

different strings which contains some

numbers and the requirement is you have

to extract the digit part from a given

string or it could be put in some other

way like how to get number part from a

given strength so this is the common

interview question which is being asked

in most of the company interviews for

automation testers so let's the we'll

see today how we can write a program how

we can write a code to extract the

digits from a given string and the

business use case or the use case from

the testing perspective is sometimes in

the while testing in the application you

get some error okay some error is there

with some let us say a page not found

error four zero four okay so you need to

validate that and you are through

automation code you are fetching that

error error message we have got and now

the requirement is you have to face the

error code from that error message which

means it's similar like string is given

and in the example I said the page note

formed either 4 0 4 so error message is

there error method is generally in this

string format and the number 4 0 4 is

digit and you need to expect that digit

well so now the question is very clear

how the question is how to expect the

digits from a given strength so let's

see the answer now and we'll write a

sample code to get the answer all right

so let's get started and let's go to the

demo straightaway

so yeah this is the Eclipse editor and

here I'll create one class plus can be

created by right click on the package on

any package you need to click on class

and I'll give the name of the class like

extract widgets well I will take a

public static void main click on finish

good

this class is created the class name is

extract Richards and I'll come in this

far

cool now we have to write a code to a to

get the number to get the number from a

given string so let's say string input

string is it's a string

page not found bass not found for this

is all right let's take another sting as

well as input to it could be internal

servers as

I'm just randomly writing on these two

strengths well now the question is you

have to write a code to fetch this

number from this string as input and the

position can be anywhere within these

strings so other scenario could be this

500 you have to fetch sometimes maybe in

the starting there are numbers okay so

the requirement is you have to fetch

these or you have to get the number part

from a given string so let's see how to

get so here is a so so far I was

explaining the problem statement I was

explaining the question now we are going

to write the actual code to get the

answer so here is the solution well it's

very simple

I can write the solution in one line

let's see how we can do that

so what I am going to do is I'll simply

I'll simply do it for this out

println

okay the error code or I should say the

number part is then here already code

number parties so what I'm gonna do is

I'll take this string as input dot

there's a method replace all hazel this

method replaced all I'll use the place

all method and it says the string

argument so here we can pass the string

here we can pass to a string parameters

replace all so it requires two string

parameters argument one argue argument

zero argument one so this part says how

to search the I mean some real we want

to replace and another parameter

parameter is what to replace okay now so

what I'm gonna do is

I'll replace all al-qaeda regular

expression now

so here just now I have written the

regular expression within a square

bracket

good let me put enter so that it is

readable

what should you figured mm okay so the

problem was spring is given and you have

to fetch the number for Oh force so what

I will do I will replace or in this

string I'll replace this part with blank

okay so I am replacing the part of the

given in input string which is not a

number so if I replace this part with a

blank only 404 will be left same way if

in this string as input - if I replace

this part and this part with blank then

we will be left with 500 so let's see

how we can put the logic so I am going

to use replace or method on this input

string as input dot replace all and

which part

so this regular expression says the

number part but we don't want to replace

the number part with blank okay

our requirement is different we want to

replace the non digit part so I will put

caret sign over here

carick sign okay so this caret sign 0 to

9 says the part of the string which is

not a number

replace that with blank so this is this

denotes blank ok so it is pretty simple

let's run the code and see whether we

are able to get the 404 the number part

of from the string or not so I'll run

all that click on this then a java

application

here you go we got the output the number

part is 404 same way let me copy this

and let's try to do the same thing for

as input 2 so for this is string as

input - I'll do the same thing in as

input to also as replace this part

internal server and this part so my

logic is as if in a given string I will

replace the part of the string which is

not a number and the after replacing the

part which is not a number without blank

I related the number so it's pretty

simple logic so same way I have done for

F input - also this is the regular

expression and with blank and replacing

let's run the code once again to see

whether we are get whether we are able

to get the number from both the strings

or not there you go well we are able to

get the numbers from a given strength so

so the question was how to affect the

digits from a string so the course the

answer it should be simple in a given

thing i will use replace all method and

i will put one regular expression which

will replace the non digit part with

blank and if the non will be at the non

digit part from a given a string is

replaced they will be left with the

digits part only well that brings me to

the end of the session now thank you so

much