development

낙타 케이스 토큰의 단어 사이에 공백 삽입

big-blog 2020. 8. 27. 08:13
반응형

낙타 케이스 토큰의 단어 사이에 공백 삽입


뭔가를 돌리는 좋은 기능이 있습니까?

이름

이에:

이름?


참조 : .NET- "대문자"로 구분 된 문자열을 배열로 분할하려면 어떻게해야합니까?

특히:

Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")

여기 제가 이런 종류의 일에 광범위하게 사용한 확장 방법이 있습니다.

public static string SplitCamelCase( this string str )
{
    return Regex.Replace( 
        Regex.Replace( 
            str, 
            @"(\P{Ll})(\P{Ll}\p{Ll})", 
            "$1 $2" 
        ), 
        @"(\p{Ll})(\P{Ll})", 
        "$1 $2" 
    );
}

또한 (IIRC) IBMMakeStuffAndSellIt로 변환하여 와 같은 문자열을 처리합니다 IBM Make Stuff And Sell It.

구문 설명 ( credit ) :

{Ll}유니 코드 문자 범주 "문자 소문자"( {Lu}"문자 대문자" 와 반대 )입니다. P는 음수 일치이고 p는 양수 일치이므로 \P{Ll}문자 그대로 "소문자가 아님"이고 p{Ll}"소문자"입니다.
따라서이 정규식은 두 패턴으로 나뉩니다. 1 : "대문자, 대문자, 소문자"( MMain IBMMake일치 하고 결과는 IBM Make) 및 2. "소문자, 대문자"( eSin에서 일치 MakeStuff). 모든 낙타 케이스 중단 점을 다룹니다.
팁 : 공백을 하이픈으로 바꾸고 ToLower를 호출하여 HTML5 데이터 속성 이름을 생성하십시오.


정규식을 사용할 수 있습니다.

Match    ([^^])([A-Z])
Replace  $1 $2

코드에서 :

String output = System.Text.RegularExpressions.Regex.Replace(
                  input,
                  "([^^])([A-Z])",
                  "$1 $2"
                );

가장 간단한 방법 :

var res = Regex.Replace("FirstName", "([A-Z])", " $1").Trim();

    /// <summary>
    /// Parse the input string by placing a space between character case changes in the string
    /// </summary>
    /// <param name="strInput">The string to parse</param>
    /// <returns>The altered string</returns>
    public static string ParseByCase(string strInput)
    {
        // The altered string (with spaces between the case changes)
        string strOutput = "";

        // The index of the current character in the input string
        int intCurrentCharPos = 0;

        // The index of the last character in the input string
        int intLastCharPos = strInput.Length - 1;

        // for every character in the input string
        for (intCurrentCharPos = 0; intCurrentCharPos <= intLastCharPos; intCurrentCharPos++)
        {
            // Get the current character from the input string
            char chrCurrentInputChar = strInput[intCurrentCharPos];

            // At first, set previous character to the current character in the input string
            char chrPreviousInputChar = chrCurrentInputChar;

            // If this is not the first character in the input string
            if (intCurrentCharPos > 0)
            {
                // Get the previous character from the input string
                chrPreviousInputChar = strInput[intCurrentCharPos - 1];

            } // end if

            // Put a space before each upper case character if the previous character is lower case
            if (char.IsUpper(chrCurrentInputChar) == true && char.IsLower(chrPreviousInputChar) == true)
            {   
                // Add a space to the output string
                strOutput += " ";

            } // end if

            // Add the character from the input string to the output string
            strOutput += chrCurrentInputChar;

        } // next

        // Return the altered string
        return strOutput;

    } // end method

정규식 :

http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx http://stackoverflow.com/questions/773303/splitting-camelcase

(probably the best - see the second answer) http://bytes.com/topic/c-sharp/answers/277768-regex-convert-camelcase-into-title-case

To convert from UpperCamelCase to Title Case, use this line : Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");

To convert from both lowerCamelCase and UpperCamelCase to Title Case, use MatchEvaluator : public string toTitleCase(Match m) { char c=m.Captures[0].Value[0]; return ((c>='a')&&(c<='z'))?Char.ToUpper(c).ToString():" "+c; } and change a little your regex with this line : Regex.Replace("UpperCamelCase or lowerCamelCase",@"(\b[a-z]|\B[A-Z])",new MatchEvaluator(toTitleCase));

참고URL : https://stackoverflow.com/questions/5796383/insert-spaces-between-words-on-a-camel-cased-token

반응형