github.com/aaabigfish/gopkg@v1.1.0/stringx/exmaple_test.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stringx
    16  
    17  import (
    18  	"fmt"
    19  	"unicode/utf8"
    20  )
    21  
    22  func Example_sub() {
    23  	fmt.Printf("Sub-[0:100]=%s\n", Sub("", 0, 100))
    24  	fmt.Printf("Sub-facgbheidjk[3:9]=%s\n", Sub("facgbheidjk", 3, 9))
    25  	fmt.Printf("Sub-facgbheidjk[-50:100]=%s\n", Sub("facgbheidjk", -50, 100))
    26  	fmt.Printf("Sub-facgbheidjk[-3:length]=%s\n", Sub("facgbheidjk", -3, utf8.RuneCountInString("facgbheidjk")))
    27  	fmt.Printf("Sub-facgbheidjk[-3:-1]=%s\n", Sub("facgbheidjk", -3, -1))
    28  	fmt.Printf("Sub-zh英文hun排[2:5]=%s\n", Sub("zh英文hun排", 2, 5))
    29  	fmt.Printf("Sub-zh英文hun排[2:-1]=%s\n", Sub("zh英文hun排", 2, -1))
    30  	fmt.Printf("Sub-zh英文hun排[-100:-1]=%s\n", Sub("zh英文hun排", -100, -1))
    31  	fmt.Printf("Sub-zh英文hun排[-100:-90]=%s\n", Sub("zh英文hun排", -100, -90))
    32  	fmt.Printf("Sub-zh英文hun排[-10:-90]=%s\n", Sub("zh英文hun排", -10, -90))
    33  
    34  	// Output:
    35  	// Sub-[0:100]=
    36  	// Sub-facgbheidjk[3:9]=gbheid
    37  	// Sub-facgbheidjk[-50:100]=facgbheidjk
    38  	// Sub-facgbheidjk[-3:length]=djk
    39  	// Sub-facgbheidjk[-3:-1]=dj
    40  	// Sub-zh英文hun排[2:5]=英文h
    41  	// Sub-zh英文hun排[2:-1]=英文hun
    42  	// Sub-zh英文hun排[-100:-1]=zh英文hun
    43  	// Sub-zh英文hun排[-100:-90]=
    44  	// Sub-zh英文hun排[-10:-90]=
    45  }
    46  
    47  func Example_substart() {
    48  	fmt.Printf("SubStart-[0:]=%s\n", SubStart("", 0))
    49  	fmt.Printf("SubStart-[2:]=%s\n", SubStart("", 2))
    50  	fmt.Printf("SubStart-facgbheidjk[3:]=%s\n", SubStart("facgbheidjk", 3))
    51  	fmt.Printf("SubStart-facgbheidjk[-50:]=%s\n", SubStart("facgbheidjk", -50))
    52  	fmt.Printf("SubStart-facgbheidjk[-3:]=%s\n", SubStart("facgbheidjk", -3))
    53  	fmt.Printf("SubStart-zh英文hun排[3:]=%s\n", SubStart("zh英文hun排", 3))
    54  
    55  	// Output:
    56  	// SubStart-[0:]=
    57  	// SubStart-[2:]=
    58  	// SubStart-facgbheidjk[3:]=gbheidjk
    59  	// SubStart-facgbheidjk[-50:]=facgbheidjk
    60  	// SubStart-facgbheidjk[-3:]=djk
    61  	// SubStart-zh英文hun排[3:]=文hun排
    62  }
    63  
    64  func Example_pad() {
    65  
    66  	fmt.Printf("PadLeft=[%s]\n", PadLeftSpace("abc", 7))
    67  	fmt.Printf("PadLeft=[%s]\n", PadLeftChar("abc", 7, '-'))
    68  	fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abc", 7, '-'))
    69  	fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abcd", 7, '-'))
    70  
    71  	// Output:
    72  	// PadLeft=[    abc]
    73  	// PadLeft=[----abc]
    74  	// PadCenter=[--abc--]
    75  	// PadCenter=[-abcd--]
    76  }