Splits a string - strtok function, MATLAB in Engineering

Splits a string :

The strtok function splits a string into pieces; it can be called in many ways. The function receives one string as an input argument. It appears for the first delimiter that is a character or set of characters which act as a separator surrounded by the string. By default, the delimiter is any whitespace character. The function returns a token that is the starting of the string, up to the first delimiter. It also returns the rest of the string, that includes the delimiter. Assigning the returned values to a vector of two variables will capture both of these. The format is as shown below:

[token rest] = strtok(string)

here token and rest are the variable names. For illustration,

 

>> sentence1 = 'Hello there'

sentence1 =

Hello there

>> [word rest] = strtok(sentence1)

word =

Hello

rest =

there

>> length(word)

ans =

5

>> length(rest)

ans =

6

 

Note that the rest of the string involves the blank space delimiter. By the default, a delimiter for the token is a whitespace character (that means the token is defined as everything up to the blank space), but alternating delimiters can be defined. The format is as shown below:

[token rest] = strtok(string, delimeters)

 

returns a token which is the starting of the string, up to the first character hold within the delimiters string as well as the rest of the string. In the illustration below, the delimiter is the character 'l'.

 

>> [word rest] = strtok(sentence1,'l')

word =

He

rest =

llo there

 

The leading delimiter characters are ignored, whether it is the specified delimiter or a default whitespace. For illustration, the leading blanks are ignored here:

 

>> [firstpart lastpart] = strtok('  materials science')

firstpart =

materials

lastpart =

science

 

Posted Date: 10/20/2012 5:36:32 AM | Location : United States







Related Discussions:- Splits a string - strtok function, Assignment Help, Ask Question on Splits a string - strtok function, Get Answer, Expert's Help, Splits a string - strtok function Discussions

Write discussion on Splits a string - strtok function
Your posts are moderated
Related Questions
Use polyval to evaluate the derivative at xder. This will be the % slope of the tangent line, "a" (general form of a line: y = ax + b). % 4. Calculate the intercept, b, of t

Implementation of binary search: The binary search can be implemented as a recursive function. The recursive function below also implements this binary search algorithm. It re

Patch function - graphics objects: The patch function is used to generate a patch graphics object, which is made from 2-dimensional polygons. The patch is defined by its verti

Defined a variable in work space: The variables defined in the script will become a part of the workspace: >> clear >> who >> mysummfile    15 >> who

Illustration of if - else statement: The one application of an if-else statement is to check for errors in the inputs to a script. For illustration, a former script prompted t

Displaying the cell arrays: There are several techniques of displaying the cell arrays. The celldisp function shows all elements of the cell array:   >> celldisp(cellro

Illustration of Matrix solutions: For illustration, consider the three equations below with 3unknowns x 1 ,x 2 , and x 3 : We can write this in the form Ax = b here A

Interpolation and extrapolation: In most cases, it is desired to estimate values other than at the sampled data points. For illustration, we may want to estimate what the temp

Matrix definitions: As we know the matrix can be thought of as a table of values in which there are both rows and columns. The most common form of a matrix A (that is sometime

Algorithm for the function e: The algorithm for the function eoption is as shown: Use the menu function to show the 4 choices. Error-check (an error would take place