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
Indexing into Vectors of structures: Frequently, when the data structure is a vector of structures, it is essential to iterate through the vector in order by various fields. F

Logical scalar values: The MATLAB also has or and and operators which work element wise for the matrices: These operators will compare any of the two vectors or matric

Example of image processing: The other illustration generates a 5 × 5 matrix of arbitrary integers in the range from 1 to the number of colors; the resultant image is as shown

deblank function: The deblank function eliminates only trailing blanks from the string, not leading the blanks. The strtrim function will eliminate both the leading and traili

Finding sums and products: A very general application of a for loop is to compute sums and products. For illustration, rather than of just printing the integers 1 through 5, w

Algorithm for subfunction: The algorithm for subfunction askforn is as shown:  Prompt the user for the positive integer n.  Loop to print an error message and reprom

Function cirarea - Anonymous functions: The function handle name is cirarea. The one argument is passed to the input argument radius. The body of the function is an expression

Sorting Vectors of structures: Whenever working with vector of structures, it is very common to sort based on a particular field within the structures. For illustration, recal

I have a vector of X, one for Y , one for x-direction velocity U and one for y-direction velocity V. they are at same size. How can I plot streamline of that flow? I follow all exa