Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elisp split-string function to split a string by . character

I am trying to split a string using 'split-string' function based on the . character. But (split-string "1.2.3" ".") doesn't work at all. It just returns a list of variable number of empty strings. Is . a special character that needs to be escaped or specified in some different way?

like image 952
Guruprasad Avatar asked Jun 04 '11 10:06

Guruprasad


People also ask

How do you split a string at a certain character?

To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split a character in a string in C #?

Splitting a string using strtok() in C In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How do I split a string into substrings?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


Video Answer


1 Answers

Here is the official documentation for split-string function - https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Strings.html

The second argument to the split-string function in (split-string "1.2.3" "\.") is a regular expression and as a result both the '.' character and '' character have special meaning. So the '.' character needs to be escaped and even the '' character needs to be escaped with another ''. (split-string "1.2.3" "\\.") will work fine as expected.

like image 51
Guruprasad Avatar answered Sep 25 '22 05:09

Guruprasad