github.com/songzhibin97/gkit@v1.2.13/sys/stringx/exmaple_test.go (about) 1 package stringx 2 3 import ( 4 "fmt" 5 "unicode/utf8" 6 ) 7 8 func Example_sub() { 9 fmt.Printf("Sub-[0:100]=%s\n", Sub("", 0, 100)) 10 fmt.Printf("Sub-facgbheidjk[3:9]=%s\n", Sub("facgbheidjk", 3, 9)) 11 fmt.Printf("Sub-facgbheidjk[-50:100]=%s\n", Sub("facgbheidjk", -50, 100)) 12 fmt.Printf("Sub-facgbheidjk[-3:length]=%s\n", Sub("facgbheidjk", -3, utf8.RuneCountInString("facgbheidjk"))) 13 fmt.Printf("Sub-facgbheidjk[-3:-1]=%s\n", Sub("facgbheidjk", -3, -1)) 14 fmt.Printf("Sub-zh英文hun排[2:5]=%s\n", Sub("zh英文hun排", 2, 5)) 15 fmt.Printf("Sub-zh英文hun排[2:-1]=%s\n", Sub("zh英文hun排", 2, -1)) 16 fmt.Printf("Sub-zh英文hun排[-100:-1]=%s\n", Sub("zh英文hun排", -100, -1)) 17 fmt.Printf("Sub-zh英文hun排[-100:-90]=%s\n", Sub("zh英文hun排", -100, -90)) 18 fmt.Printf("Sub-zh英文hun排[-10:-90]=%s\n", Sub("zh英文hun排", -10, -90)) 19 20 // Output: 21 // Sub-[0:100]= 22 // Sub-facgbheidjk[3:9]=gbheid 23 // Sub-facgbheidjk[-50:100]=facgbheidjk 24 // Sub-facgbheidjk[-3:length]=djk 25 // Sub-facgbheidjk[-3:-1]=dj 26 // Sub-zh英文hun排[2:5]=英文h 27 // Sub-zh英文hun排[2:-1]=英文hun 28 // Sub-zh英文hun排[-100:-1]=zh英文hun 29 // Sub-zh英文hun排[-100:-90]= 30 // Sub-zh英文hun排[-10:-90]= 31 } 32 33 func Example_substart() { 34 fmt.Printf("SubStart-[0:]=%s\n", SubStart("", 0)) 35 fmt.Printf("SubStart-[2:]=%s\n", SubStart("", 2)) 36 fmt.Printf("SubStart-facgbheidjk[3:]=%s\n", SubStart("facgbheidjk", 3)) 37 fmt.Printf("SubStart-facgbheidjk[-50:]=%s\n", SubStart("facgbheidjk", -50)) 38 fmt.Printf("SubStart-facgbheidjk[-3:]=%s\n", SubStart("facgbheidjk", -3)) 39 fmt.Printf("SubStart-zh英文hun排[3:]=%s\n", SubStart("zh英文hun排", 3)) 40 41 // Output: 42 // SubStart-[0:]= 43 // SubStart-[2:]= 44 // SubStart-facgbheidjk[3:]=gbheidjk 45 // SubStart-facgbheidjk[-50:]=facgbheidjk 46 // SubStart-facgbheidjk[-3:]=djk 47 // SubStart-zh英文hun排[3:]=文hun排 48 } 49 50 func Example_pad() { 51 52 fmt.Printf("PadLeft=[%s]\n", PadLeftSpace("abc", 7)) 53 fmt.Printf("PadLeft=[%s]\n", PadLeftChar("abc", 7, '-')) 54 fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abc", 7, '-')) 55 fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abcd", 7, '-')) 56 57 // Output: 58 // PadLeft=[ abc] 59 // PadLeft=[----abc] 60 // PadCenter=[--abc--] 61 // PadCenter=[-abcd--] 62 }