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
Illustration of gauss-jordan elimination: An illustration of interchanging rows would be r1 ¬→ r3, that would results: Now, beginning with this matrix, an illustration of sc

Function used in binary search: The function below implements this binary search algorithm. It receives two arguments: the sorted vector and a key (on the other hand, the func

Function strncmp: The function strncmp compares only the first n characters in the strings and ignores the rest. The initial two arguments are strings to compare, and third ar

Removing Whitespace Characters: The MATLAB has functions which will eliminate trailing blanks from the end of a string and/or leading blanks from the starting of a string.

How can I use the weighted moving average formula in matlab to smooth a column data of 404 values?

Program of passing arguments to functions: This was an illustration of a function which did not receive any input arguments nor did it return any output arguments; it easily a

Converting between the String and Number types: The MATLAB has many functions which convert numbers to strings in which each character element is a separate digit, and vice ve

Function rmfield - structure: The function rmfield eliminates a field from the structure. It returns a new structure with field eliminated, but does not modify the original st

Comparing strings: There are few functions which compare strings and return logical true when they are equivalent or logical false when not. The function strcmp compares the s

Square Matrices: If a matrix has similar number of rows and columns, for illustration, if m == n, the matrix is square matrix. The definitions which follow in this part apply