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 =


rest =


>> length(word)

ans =


>> length(rest)

ans =



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 =


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 =


lastpart =



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
For Loops which do not use an iterator Variable in the action: In all the illustrations that we seen so far, the value of the loop variable has been used in same way in the ac

Referring to and Showing Cell Array Elements and Attributes: Just as with the other vectors, we can refer to individual elements of the cell arrays. The only difference is tha

Text graphic function - Graphics objects: The text graphic function permits text to be printed in a Figure Window, involving special characters which are printed by using \spe

Function issorted - set operations: The function issorted will return 1 for logical true when the argument is sorted in ascending order (minimum to maximum), or 0 for false wh

Algorithm for the function explaine: The algorithm for the function explaine is as shown:  Print a description of e, the exp function, and how to find the approximate va

Print from the structure: To print from the structure, a disp function will show either the whole structure or a field. >> disp(package) item_no: 123 cost: 19.99

ischar function: The ischar function return the logical true if an array is a character array, or logical false if not. >> vec = 'EK127'; >> ischar(vec) ans =

Expanding a function: The expand function will multiply out terms, and factor will do the opposite: >> expand((x+2)*(x-1)) ans = x^2 x-2 >> factor(ans)

Graphics Properties: The MATLAB uses the Handle Graphics in all its figures. All figures consist of various objects, each of which is assigned a handle. The object handle is a