2012年11月9日 星期五

Scilab fundamentals 14 - Strings part 2

    Scilab is capable of processing strings such as combination, conversion, and separation. These functions listed in Table 1 that are useful in programming. The functions including ascii, strtod, and strcmp, were introduced in the previous section. This asection addresses on the rest functions.


²  +: combine strings
   
    The operator + combines two or more strings.

 
²  strsplit: split a string into a column vector of strings

    The function strsplit splits a string into a column vector of strings with or without a specified separator. The following illustrates the split of a string with different specified separator.

    Case 1 shows that the string A is split into individual characters when no separator is specified. In case 2, the string A is split into 3 substrings, ‘Welcome’, ‘to’, and ‘Taiwan’. Compared with the case 2, case 3 shows that the string A was not split because the specified separator was not found in the string. The case 4 and case 5 shows the splits with the separators defined by the characters ‘W’ and ‘w’, respectively.
    Here is a question. Based on the result of case 2 in example 1, can we use strsplit to split a string into individual words with a space separator, and remove all white spaces at the same time? Check the below example.


 
    The string A1 is a sentence with the separator of a white space. Therefore, the split of A1 is the same with what we expected. However, the problem arises when the separators in a string are more than one white space. The split of A2 illustrates this situation and corresponding result. It means that only one white space is recognized as a separator per time. The differences between A1 and A2 are shown in Fig. 1.


²  tokens: returns the tokens of a character sting
 
    The function tokens returns the tokens of a character sting. In other words, this function separates a string into individual words and deletes all white spaces.
 
    As shown in the above example, the dimensions of Out_A1 and Out_A2 are same. The elements of Out_A1 and Out_A2 are listed in Table 2.



沒有留言:

張貼留言