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
Scaling:   change a row by multiplying it by a non-zero scalar sri →  ri For illustration, for the matrix:

Example of file ploting data: As the other example, a data file called 'compsales.dat' stores the sales figures (in millions) for divisions in a company. Each line in the f

Illustration of Sound files: For illustration, the following script generates a subplot which shows the signals from chirp and from train, which is as shown in figure:

Application: Menu driven Modular Program Numerous longer, more involved programs which have interaction with the user are menu-driven, that means that the program prints a men

Execute a exponential function program: Running the script will take up the menu as shown in the figure: Then, what happens will totally depend on which button(s) the

Dot Product: The dot or inner product of two vectors a and b is written as a • b and is defined as  In another words, this is like matrix multiplication when multiplyi


Replacing a string - function strrep: The function strrep finds all the occurrences of a substring within the string, and substitutes them with a new substring. The order of a

Example of Gauss-jordan: For a 2×2 system, this would results and for a 3 × 3 system, Note that the resulting diagonal form does not involve the right-most col

Modular programs: In a modular program, the answer is broken down into modules, and each is executed as a function. The script is usually known as the main program. In orde