Example of converting the camper string into an underline style for a PHP interview question
When I saw this problem, I thought of using ASCII code to handle it. I didn't think about it as a universal regular expression. Okay, let's take a look at the answer:
Answer 1:
The Code is as follows:
$ Str = 'openapi ';
$ Length = mb_strlen ($ str );
$ New = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
$ Num = ord ($ str [$ I]);
$ Pre = ord ($ str [$ I-1]);
$ New. = ($ I! = 0 & ($ num >=65 & $ num <= 90) & ($ pre >=97 & $ pre <= 122 ))? "_ {$ Str [$ I]}": $ str [$ I];
} Www.111cn.net
Echo strtolower ($ new). '<br> ';
Answer 2:
The Code is as follows:
Echo strtolower (preg_replace ('/((? <= [A-z]) (? = [A-Z])/',' _ ', $ str).' <br> ';
Then, in turn, how can I convert the underlined string into a camel (www.111cn.net) peak string?
The Code is as follows:
F = new File ("d:/temp/t.txt ")
If (f. exists ()){
F. eachLine {line->
Line = line. trim ()
String [] elems = line. split ('_')
For (int I = 0; I <elems. length; I ++ ){
Elems [I] = elems [I]. toLowerCase ()
If (I! = 0 ){
String elem = elems [I]
Char first = elem [0] as char
Elems [I] = "" + (char) (first-32) + elem. substring (1)
}
}
Println elems. join ()
}
}
From: http://www.111cn.net/phper/php-cy/59093.htm
How can we convert string styles from camper to underline in java?
Public class transform {public static String trans (String str) {List record = new ArrayList (); for (int I = 0; I <str. length (); I ++) {char tmp = str. charAt (I); if (tmp <= 'Z') & (tmp> = 'A') {record. add (I); // records the location of each uppercase letter} record. remove (0); // The first one does not need to be underlined str = str. toLowerCase (); char [] charofstr = str. toCharArray (); String [] t = new String [record. size ()]; for (int I = 0; I <record. size (); I ++) {t [I] = "_" + charofstr [(int) record. get (I)]; // Add "_"} String result = ""; int flag = 0; for (int I = 0; I <str. length (); I ++) {if (flag <record. size () & (I = (int) record. get (flag) {result + = t [flag]; flag ++;} elseresult + = charofstr [I];} return result ;} public static void main (String [] args) {String hh = "HelloWolrd"; System. out. println (trans (hh ));}}
Php Regular Expression how to convert all spaces in a string into underscores
The simple code is as follows:
Preg_replace ('/[\ s]/', '_', $ content); // replace spaces, including spaces. If you have any questions, contact us ~