Spinal Tap case

Source: Internet
Author: User

Topic

Converts a string to a spinal case. Spinal case is all-lowercase-words-joined-by-dashes in this form, that is, connecting all lowercase words with hyphens.

Test:

Spinalcase ("This Is Spinal Tap") should return "This-is-spinal-tap".
Spinalcase ("Thisisspinaltap") should return "This-is-spinal-tap".
Spinalcase ("The_andy_griffith_show") should return "The-andy-griffith-show".
Spinalcase ("Teletubbies say Eh-oh") should return "Teletubbies-say-eh-oh".

Ideas

Looking at the test case, the string format that needs to be converted can be categorized into two categories. The first category is the use of spaces, underscores and other symbolic decomposition of the name, and the second class is familiar with the hump naming law. The following are called symbol types or hump types.

The symbolic type conversion form is simple, the Hump type conversion form is also simple, but this is not the same, how to distinguish between the two is a difficult point. We can first set a small target that can be achieved, such as separating the two string types first. The blogger's method is to divide the string by the symbol type, if the decomposed array length of 1 also shows that the current string, whether or not serious, he is a hump type string.

Str.split (/\w|_/). Length==1

Since the two types of strings are separated, the two types of strings are converted to the subject: I-----Wu-yan-zu this type.

Hump type conversion is very simple, only need to go through the string, use regular expressions to determine whether a character in a string is uppercase, if it is uppercase to lowercase and then add a short thing in front. Here's how:

1      for (var i=0;i<str.length;i++) {2       if (/[a-z]/. Test (Str[i])) {3         str=str.replace (Str[i], "-" +str[i].tolowercase ()); 4       }5     }

The symbol type is simpler, just convert the string to lowercase and use a regular expression to detach and then use a single thing to connect between each of the two.

str=str.tolowercase (). Split (/\w|_/). Join ("-");

The overall code is as follows:

1 functionspinalcase (str) {2   if(Str.split (/\w|_/). length==1){3      for(vari=0;i<str.length;i++){4       if(/[a-z]/. Test (Str[i])) {5Str=str.replace (Str[i], "-" +str[i].tolowercase ());6       }7     }8}ElseStr=str.tolowercase (). Split (/\w|_/). Join ("-");9   returnstr;Ten}

Spinal Tap case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.