129k views
5 votes
Write a sequence of statements that finds the first comma in the string line, and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos, as well as the variables line and clause, have already been declared.

User Dmnkhhn
by
8.0k points

1 Answer

4 votes

Answer:

I will code in Javascript;

function findFirstComma() {

var pos;

var line = 'Thi,s is a t,est';

var clause;

pos = line.indexOf(','); //set pos in 3.

clause= line.slice(0,pos); // saves in clause the value 'Thi'.

}

Step-by-step explanation:

The slice(start, end) method extract a part of a string then returns a new string with the extracted part. The end parameter indicates where to end the extraction(up to, but not including).

The includes(value) method determines if a string contains the characters on a specified string, if it doesn't match then returns -1.

User Tianissimo
by
8.3k points