github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_z_example_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gstr_test 8 9 import ( 10 "fmt" 11 12 "github.com/gogf/gf/v2/text/gstr" 13 ) 14 15 func ExampleCount() { 16 var ( 17 str = `goframe is very, very easy to use` 18 substr1 = "goframe" 19 substr2 = "very" 20 result1 = gstr.Count(str, substr1) 21 result2 = gstr.Count(str, substr2) 22 ) 23 fmt.Println(result1) 24 fmt.Println(result2) 25 26 // Output: 27 // 1 28 // 2 29 } 30 31 func ExampleCountI() { 32 var ( 33 str = `goframe is very, very easy to use` 34 substr1 = "GOFRAME" 35 substr2 = "VERY" 36 result1 = gstr.CountI(str, substr1) 37 result2 = gstr.CountI(str, substr2) 38 ) 39 fmt.Println(result1) 40 fmt.Println(result2) 41 42 // Output: 43 // 1 44 // 2 45 } 46 47 func ExampleToLower() { 48 var ( 49 s = `GOFRAME` 50 result = gstr.ToLower(s) 51 ) 52 fmt.Println(result) 53 54 // Output: 55 // goframe 56 } 57 58 func ExampleToUpper() { 59 var ( 60 s = `goframe` 61 result = gstr.ToUpper(s) 62 ) 63 fmt.Println(result) 64 65 // Output: 66 // GOFRAME 67 } 68 69 func ExampleUcFirst() { 70 var ( 71 s = `hello` 72 result = gstr.UcFirst(s) 73 ) 74 fmt.Println(result) 75 76 // Output: 77 // Hello 78 } 79 80 func ExampleLcFirst() { 81 var ( 82 str = `Goframe` 83 result = gstr.LcFirst(str) 84 ) 85 fmt.Println(result) 86 87 // Output: 88 // goframe 89 } 90 91 func ExampleUcWords() { 92 var ( 93 str = `hello world` 94 result = gstr.UcWords(str) 95 ) 96 fmt.Println(result) 97 98 // Output: 99 // Hello World 100 } 101 102 func ExampleIsLetterLower() { 103 fmt.Println(gstr.IsLetterLower('a')) 104 fmt.Println(gstr.IsLetterLower('A')) 105 106 // Output: 107 // true 108 // false 109 } 110 111 func ExampleIsLetterUpper() { 112 fmt.Println(gstr.IsLetterUpper('A')) 113 fmt.Println(gstr.IsLetterUpper('a')) 114 115 // Output: 116 // true 117 // false 118 } 119 120 func ExampleIsNumeric() { 121 fmt.Println(gstr.IsNumeric("88")) 122 fmt.Println(gstr.IsNumeric("3.1415926")) 123 fmt.Println(gstr.IsNumeric("abc")) 124 // Output: 125 // true 126 // true 127 // false 128 } 129 130 func ExampleReverse() { 131 var ( 132 str = `123456` 133 result = gstr.Reverse(str) 134 ) 135 fmt.Println(result) 136 137 // Output: 138 // 654321 139 } 140 141 func ExampleNumberFormat() { 142 var ( 143 number float64 = 123456 144 decimals = 2 145 decPoint = "." 146 thousandsSep = "," 147 result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) 148 ) 149 fmt.Println(result) 150 151 // Output: 152 // 123,456.00 153 } 154 155 func ExampleChunkSplit() { 156 var ( 157 body = `1234567890` 158 chunkLen = 2 159 end = "#" 160 result = gstr.ChunkSplit(body, chunkLen, end) 161 ) 162 fmt.Println(result) 163 164 // Output: 165 // 12#34#56#78#90# 166 } 167 168 func ExampleCompare() { 169 fmt.Println(gstr.Compare("c", "c")) 170 fmt.Println(gstr.Compare("a", "b")) 171 fmt.Println(gstr.Compare("c", "b")) 172 173 // Output: 174 // 0 175 // -1 176 // 1 177 } 178 179 func ExampleEqual() { 180 fmt.Println(gstr.Equal(`A`, `a`)) 181 fmt.Println(gstr.Equal(`A`, `A`)) 182 fmt.Println(gstr.Equal(`A`, `B`)) 183 184 // Output: 185 // true 186 // true 187 // false 188 } 189 190 func ExampleFields() { 191 var ( 192 str = `Hello World` 193 result = gstr.Fields(str) 194 ) 195 fmt.Printf(`%#v`, result) 196 197 // Output: 198 // []string{"Hello", "World"} 199 } 200 201 func ExampleHasPrefix() { 202 var ( 203 s = `Hello World` 204 prefix = "Hello" 205 result = gstr.HasPrefix(s, prefix) 206 ) 207 fmt.Println(result) 208 209 // Output: 210 // true 211 } 212 213 func ExampleHasSuffix() { 214 var ( 215 s = `my best love is goframe` 216 prefix = "goframe" 217 result = gstr.HasSuffix(s, prefix) 218 ) 219 fmt.Println(result) 220 221 // Output: 222 // true 223 } 224 225 func ExampleCountWords() { 226 var ( 227 str = `goframe is very, very easy to use!` 228 result = gstr.CountWords(str) 229 ) 230 fmt.Printf(`%#v`, result) 231 232 // Output: 233 // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1} 234 } 235 236 func ExampleCountChars() { 237 var ( 238 str = `goframe` 239 result = gstr.CountChars(str) 240 ) 241 fmt.Println(result) 242 243 // May Output: 244 // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] 245 } 246 247 func ExampleWordWrap() { 248 { 249 var ( 250 str = `A very long woooooooooooooooooord. and something` 251 width = 8 252 br = "\n" 253 result = gstr.WordWrap(str, width, br) 254 ) 255 fmt.Println(result) 256 } 257 { 258 var ( 259 str = `The quick brown fox jumped over the lazy dog.` 260 width = 20 261 br = "<br />\n" 262 result = gstr.WordWrap(str, width, br) 263 ) 264 fmt.Printf("%v", result) 265 } 266 267 // Output: 268 // A very 269 // long 270 // woooooooooooooooooord. 271 // and 272 // something 273 // The quick brown fox<br /> 274 // jumped over the lazy<br /> 275 // dog. 276 } 277 278 func ExampleLenRune() { 279 var ( 280 str = `GoFrame框架` 281 result = gstr.LenRune(str) 282 ) 283 fmt.Println(result) 284 285 // Output: 286 // 9 287 } 288 289 func ExampleRepeat() { 290 var ( 291 input = `goframe ` 292 multiplier = 3 293 result = gstr.Repeat(input, multiplier) 294 ) 295 fmt.Println(result) 296 297 // Output: 298 // goframe goframe goframe 299 } 300 301 func ExampleShuffle() { 302 var ( 303 str = `123456` 304 result = gstr.Shuffle(str) 305 ) 306 fmt.Println(result) 307 308 // May Output: 309 // 563214 310 } 311 312 func ExampleSplit() { 313 var ( 314 str = `a|b|c|d` 315 delimiter = `|` 316 result = gstr.Split(str, delimiter) 317 ) 318 fmt.Printf(`%#v`, result) 319 320 // Output: 321 // []string{"a", "b", "c", "d"} 322 } 323 324 func ExampleSplitAndTrim() { 325 var ( 326 str = `a|b|||||c|d` 327 delimiter = `|` 328 result = gstr.SplitAndTrim(str, delimiter) 329 ) 330 fmt.Printf(`%#v`, result) 331 332 // Output: 333 // []string{"a", "b", "c", "d"} 334 } 335 336 func ExampleJoin() { 337 var ( 338 array = []string{"goframe", "is", "very", "easy", "to", "use"} 339 sep = ` ` 340 result = gstr.Join(array, sep) 341 ) 342 fmt.Println(result) 343 344 // Output: 345 // goframe is very easy to use 346 } 347 348 func ExampleJoinAny() { 349 var ( 350 sep = `,` 351 arr2 = []int{99, 73, 85, 66} 352 result = gstr.JoinAny(arr2, sep) 353 ) 354 fmt.Println(result) 355 356 // Output: 357 // 99,73,85,66 358 } 359 360 func ExampleExplode() { 361 var ( 362 str = `Hello World` 363 delimiter = " " 364 result = gstr.Explode(delimiter, str) 365 ) 366 fmt.Printf(`%#v`, result) 367 368 // Output: 369 // []string{"Hello", "World"} 370 } 371 372 func ExampleImplode() { 373 var ( 374 pieces = []string{"goframe", "is", "very", "easy", "to", "use"} 375 glue = " " 376 result = gstr.Implode(glue, pieces) 377 ) 378 fmt.Println(result) 379 380 // Output: 381 // goframe is very easy to use 382 } 383 384 func ExampleChr() { 385 var ( 386 ascii = 65 // A 387 result = gstr.Chr(ascii) 388 ) 389 fmt.Println(result) 390 391 // Output: 392 // A 393 } 394 395 // '103' is the 'g' in ASCII 396 func ExampleOrd() { 397 var ( 398 str = `goframe` 399 result = gstr.Ord(str) 400 ) 401 402 fmt.Println(result) 403 404 // Output: 405 // 103 406 } 407 408 func ExampleHideStr() { 409 var ( 410 str = `13800138000` 411 percent = 40 412 hide = `*` 413 result = gstr.HideStr(str, percent, hide) 414 ) 415 fmt.Println(result) 416 417 // Output: 418 // 138****8000 419 } 420 421 func ExampleNl2Br() { 422 var ( 423 str = `goframe 424 is 425 very 426 easy 427 to 428 use` 429 result = gstr.Nl2Br(str) 430 ) 431 432 fmt.Println(result) 433 434 // Output: 435 // goframe<br>is<br>very<br>easy<br>to<br>use 436 } 437 438 func ExampleAddSlashes() { 439 var ( 440 str = `'aa'"bb"cc\r\n\d\t` 441 result = gstr.AddSlashes(str) 442 ) 443 444 fmt.Println(result) 445 446 // Output: 447 // \'aa\'\"bb\"cc\\r\\n\\d\\t 448 } 449 450 func ExampleStripSlashes() { 451 var ( 452 str = `C:\\windows\\GoFrame\\test` 453 result = gstr.StripSlashes(str) 454 ) 455 fmt.Println(result) 456 457 // Output: 458 // C:\windows\GoFrame\test 459 } 460 461 func ExampleQuoteMeta() { 462 { 463 var ( 464 str = `.\+?[^]()` 465 result = gstr.QuoteMeta(str) 466 ) 467 fmt.Println(result) 468 } 469 { 470 var ( 471 str = `https://goframe.org/pages/viewpage.action?pageId=1114327` 472 result = gstr.QuoteMeta(str) 473 ) 474 fmt.Println(result) 475 } 476 477 // Output: 478 // \.\\\+\?\[\^\]\(\) 479 // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 480 481 } 482 483 // array 484 func ExampleSearchArray() { 485 var ( 486 array = []string{"goframe", "is", "very", "nice"} 487 str = `goframe` 488 result = gstr.SearchArray(array, str) 489 ) 490 fmt.Println(result) 491 492 // Output: 493 // 0 494 } 495 496 func ExampleInArray() { 497 var ( 498 a = []string{"goframe", "is", "very", "easy", "to", "use"} 499 s = "goframe" 500 result = gstr.InArray(a, s) 501 ) 502 fmt.Println(result) 503 504 // Output: 505 // true 506 } 507 508 func ExamplePrefixArray() { 509 var ( 510 strArray = []string{"tom", "lily", "john"} 511 ) 512 513 gstr.PrefixArray(strArray, "classA_") 514 515 fmt.Println(strArray) 516 517 // Output: 518 // [classA_tom classA_lily classA_john] 519 } 520 521 // case 522 func ExampleCaseCamel() { 523 var ( 524 str = `hello world` 525 result = gstr.CaseCamel(str) 526 ) 527 fmt.Println(result) 528 529 // Output: 530 // HelloWorld 531 } 532 533 func ExampleCaseCamelLower() { 534 var ( 535 str = `hello world` 536 result = gstr.CaseCamelLower(str) 537 ) 538 fmt.Println(result) 539 540 // Output: 541 // helloWorld 542 } 543 544 func ExampleCaseSnake() { 545 var ( 546 str = `hello world` 547 result = gstr.CaseSnake(str) 548 ) 549 fmt.Println(result) 550 551 // Output: 552 // hello_world 553 } 554 555 func ExampleCaseSnakeScreaming() { 556 var ( 557 str = `hello world` 558 result = gstr.CaseSnakeScreaming(str) 559 ) 560 fmt.Println(result) 561 562 // Output: 563 // HELLO_WORLD 564 } 565 566 func ExampleCaseSnakeFirstUpper() { 567 var ( 568 str = `RGBCodeMd5` 569 result = gstr.CaseSnakeFirstUpper(str) 570 ) 571 fmt.Println(result) 572 573 // Output: 574 // rgb_code_md5 575 } 576 577 func ExampleCaseKebab() { 578 var ( 579 str = `hello world` 580 result = gstr.CaseKebab(str) 581 ) 582 fmt.Println(result) 583 584 // Output: 585 // hello-world 586 } 587 588 func ExampleCaseKebabScreaming() { 589 var ( 590 str = `hello world` 591 result = gstr.CaseKebabScreaming(str) 592 ) 593 fmt.Println(result) 594 595 // Output: 596 // HELLO-WORLD 597 } 598 599 func ExampleCaseDelimited() { 600 var ( 601 str = `hello world` 602 del = byte('-') 603 result = gstr.CaseDelimited(str, del) 604 ) 605 fmt.Println(result) 606 607 // Output: 608 // hello-world 609 } 610 611 func ExampleCaseDelimitedScreaming() { 612 { 613 var ( 614 str = `hello world` 615 del = byte('-') 616 result = gstr.CaseDelimitedScreaming(str, del, true) 617 ) 618 fmt.Println(result) 619 } 620 { 621 var ( 622 str = `hello world` 623 del = byte('-') 624 result = gstr.CaseDelimitedScreaming(str, del, false) 625 ) 626 fmt.Println(result) 627 } 628 629 // Output: 630 // HELLO-WORLD 631 // hello-world 632 } 633 634 // contain 635 func ExampleContains() { 636 { 637 var ( 638 str = `Hello World` 639 substr = `Hello` 640 result = gstr.Contains(str, substr) 641 ) 642 fmt.Println(result) 643 } 644 { 645 var ( 646 str = `Hello World` 647 substr = `hello` 648 result = gstr.Contains(str, substr) 649 ) 650 fmt.Println(result) 651 } 652 653 // Output: 654 // true 655 // false 656 } 657 658 func ExampleContainsI() { 659 var ( 660 str = `Hello World` 661 substr = "hello" 662 result1 = gstr.Contains(str, substr) 663 result2 = gstr.ContainsI(str, substr) 664 ) 665 fmt.Println(result1) 666 fmt.Println(result2) 667 668 // Output: 669 // false 670 // true 671 } 672 673 func ExampleContainsAny() { 674 { 675 var ( 676 s = `goframe` 677 chars = "g" 678 result = gstr.ContainsAny(s, chars) 679 ) 680 fmt.Println(result) 681 } 682 { 683 var ( 684 s = `goframe` 685 chars = "G" 686 result = gstr.ContainsAny(s, chars) 687 ) 688 fmt.Println(result) 689 } 690 691 // Output: 692 // true 693 // false 694 } 695 696 // convert 697 func ExampleOctStr() { 698 var ( 699 str = `\346\200\241` 700 result = gstr.OctStr(str) 701 ) 702 fmt.Println(result) 703 704 // Output: 705 // 怡 706 } 707 708 // domain 709 func ExampleIsSubDomain() { 710 var ( 711 subDomain = `s.goframe.org` 712 mainDomain = `goframe.org` 713 result = gstr.IsSubDomain(subDomain, mainDomain) 714 ) 715 fmt.Println(result) 716 717 // Output: 718 // true 719 } 720 721 // levenshtein 722 func ExampleLevenshtein() { 723 var ( 724 str1 = "Hello World" 725 str2 = "hallo World" 726 costIns = 1 727 costRep = 1 728 costDel = 1 729 result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel) 730 ) 731 fmt.Println(result) 732 733 // Output: 734 // 2 735 } 736 737 // parse 738 func ExampleParse() { 739 { 740 var ( 741 str = `v1=m&v2=n` 742 result, _ = gstr.Parse(str) 743 ) 744 fmt.Println(result) 745 } 746 { 747 var ( 748 str = `v[a][a]=m&v[a][b]=n` 749 result, _ = gstr.Parse(str) 750 ) 751 fmt.Println(result) 752 } 753 { 754 // The form of nested Slice is not yet supported. 755 var str = `v[][]=m&v[][]=n` 756 result, err := gstr.Parse(str) 757 if err != nil { 758 panic(err) 759 } 760 fmt.Println(result) 761 } 762 { 763 // This will produce an error. 764 var str = `v=m&v[a]=n` 765 result, err := gstr.Parse(str) 766 if err != nil { 767 println(err) 768 } 769 fmt.Println(result) 770 } 771 { 772 var ( 773 str = `a .[[b=c` 774 result, _ = gstr.Parse(str) 775 ) 776 fmt.Println(result) 777 } 778 779 // May Output: 780 // map[v1:m v2:n] 781 // map[v:map[a:map[a:m b:n]]] 782 // map[v:map[]] 783 // Error: expected type 'map[string]interface{}' for key 'v', but got 'string' 784 // map[] 785 // map[a___[b:c] 786 } 787 788 // pos 789 func ExamplePos() { 790 var ( 791 haystack = `Hello World` 792 needle = `World` 793 result = gstr.Pos(haystack, needle) 794 ) 795 fmt.Println(result) 796 797 // Output: 798 // 6 799 } 800 801 func ExamplePosRune() { 802 var ( 803 haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` 804 needle = `Go` 805 posI = gstr.PosRune(haystack, needle) 806 posR = gstr.PosRRune(haystack, needle) 807 ) 808 fmt.Println(posI) 809 fmt.Println(posR) 810 811 // Output: 812 // 0 813 // 22 814 } 815 816 func ExamplePosI() { 817 var ( 818 haystack = `goframe is very, very easy to use` 819 needle = `very` 820 posI = gstr.PosI(haystack, needle) 821 posR = gstr.PosR(haystack, needle) 822 ) 823 fmt.Println(posI) 824 fmt.Println(posR) 825 826 // Output: 827 // 11 828 // 17 829 } 830 831 func ExamplePosIRune() { 832 { 833 var ( 834 haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` 835 needle = `高性能` 836 startOffset = 10 837 result = gstr.PosIRune(haystack, needle, startOffset) 838 ) 839 fmt.Println(result) 840 } 841 { 842 var ( 843 haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` 844 needle = `高性能` 845 startOffset = 30 846 result = gstr.PosIRune(haystack, needle, startOffset) 847 ) 848 fmt.Println(result) 849 } 850 851 // Output: 852 // 14 853 // -1 854 } 855 856 func ExamplePosR() { 857 var ( 858 haystack = `goframe is very, very easy to use` 859 needle = `very` 860 posI = gstr.PosI(haystack, needle) 861 posR = gstr.PosR(haystack, needle) 862 ) 863 fmt.Println(posI) 864 fmt.Println(posR) 865 866 // Output: 867 // 11 868 // 17 869 } 870 871 func ExamplePosRRune() { 872 var ( 873 haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` 874 needle = `Go` 875 posI = gstr.PosIRune(haystack, needle) 876 posR = gstr.PosRRune(haystack, needle) 877 ) 878 fmt.Println(posI) 879 fmt.Println(posR) 880 881 // Output: 882 // 0 883 // 22 884 } 885 886 func ExamplePosRI() { 887 var ( 888 haystack = `goframe is very, very easy to use` 889 needle = `VERY` 890 posI = gstr.PosI(haystack, needle) 891 posR = gstr.PosRI(haystack, needle) 892 ) 893 fmt.Println(posI) 894 fmt.Println(posR) 895 896 // Output: 897 // 11 898 // 17 899 } 900 901 func ExamplePosRIRune() { 902 var ( 903 haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` 904 needle = `GO` 905 posI = gstr.PosIRune(haystack, needle) 906 posR = gstr.PosRIRune(haystack, needle) 907 ) 908 fmt.Println(posI) 909 fmt.Println(posR) 910 911 // Output: 912 // 0 913 // 22 914 } 915 916 // replace 917 func ExampleReplace() { 918 var ( 919 origin = `golang is very nice!` 920 search = `golang` 921 replace = `goframe` 922 result = gstr.Replace(origin, search, replace) 923 ) 924 fmt.Println(result) 925 926 // Output: 927 // goframe is very nice! 928 } 929 930 func ExampleReplaceI() { 931 var ( 932 origin = `golang is very nice!` 933 search = `GOLANG` 934 replace = `goframe` 935 result = gstr.ReplaceI(origin, search, replace) 936 ) 937 fmt.Println(result) 938 939 // Output: 940 // goframe is very nice! 941 } 942 943 func ExampleReplaceByArray() { 944 { 945 var ( 946 origin = `golang is very nice` 947 array = []string{"lang", "frame"} 948 result = gstr.ReplaceByArray(origin, array) 949 ) 950 fmt.Println(result) 951 } 952 { 953 var ( 954 origin = `golang is very good` 955 array = []string{"golang", "goframe", "good", "nice"} 956 result = gstr.ReplaceByArray(origin, array) 957 ) 958 fmt.Println(result) 959 } 960 961 // Output: 962 // goframe is very nice 963 // goframe is very nice 964 } 965 966 func ExampleReplaceIByArray() { 967 var ( 968 origin = `golang is very Good` 969 array = []string{"Golang", "goframe", "GOOD", "nice"} 970 result = gstr.ReplaceIByArray(origin, array) 971 ) 972 973 fmt.Println(result) 974 975 // Output: 976 // goframe is very nice 977 } 978 979 func ExampleReplaceByMap() { 980 { 981 var ( 982 origin = `golang is very nice` 983 replaces = map[string]string{ 984 "lang": "frame", 985 } 986 result = gstr.ReplaceByMap(origin, replaces) 987 ) 988 fmt.Println(result) 989 } 990 { 991 var ( 992 origin = `golang is very good` 993 replaces = map[string]string{ 994 "golang": "goframe", 995 "good": "nice", 996 } 997 result = gstr.ReplaceByMap(origin, replaces) 998 ) 999 fmt.Println(result) 1000 } 1001 1002 // Output: 1003 // goframe is very nice 1004 // goframe is very nice 1005 } 1006 1007 func ExampleReplaceIByMap() { 1008 var ( 1009 origin = `golang is very nice` 1010 replaces = map[string]string{ 1011 "Lang": "frame", 1012 } 1013 result = gstr.ReplaceIByMap(origin, replaces) 1014 ) 1015 fmt.Println(result) 1016 1017 // Output: 1018 // goframe is very nice 1019 } 1020 1021 // similartext 1022 func ExampleSimilarText() { 1023 var ( 1024 first = `AaBbCcDd` 1025 second = `ad` 1026 percent = 0.80 1027 result = gstr.SimilarText(first, second, &percent) 1028 ) 1029 fmt.Println(result) 1030 1031 // Output: 1032 // 2 1033 } 1034 1035 // soundex 1036 func ExampleSoundex() { 1037 var ( 1038 str1 = `Hello` 1039 str2 = `Hallo` 1040 result1 = gstr.Soundex(str1) 1041 result2 = gstr.Soundex(str2) 1042 ) 1043 fmt.Println(result1, result2) 1044 1045 // Output: 1046 // H400 H400 1047 } 1048 1049 // str 1050 func ExampleStr() { 1051 var ( 1052 haystack = `xxx.jpg` 1053 needle = `.` 1054 result = gstr.Str(haystack, needle) 1055 ) 1056 fmt.Println(result) 1057 1058 // Output: 1059 // .jpg 1060 } 1061 1062 func ExampleStrEx() { 1063 var ( 1064 haystack = `https://goframe.org/index.html?a=1&b=2` 1065 needle = `?` 1066 result = gstr.StrEx(haystack, needle) 1067 ) 1068 fmt.Println(result) 1069 1070 // Output: 1071 // a=1&b=2 1072 } 1073 1074 func ExampleStrTill() { 1075 var ( 1076 haystack = `https://goframe.org/index.html?test=123456` 1077 needle = `?` 1078 result = gstr.StrTill(haystack, needle) 1079 ) 1080 fmt.Println(result) 1081 1082 // Output: 1083 // https://goframe.org/index.html? 1084 } 1085 1086 func ExampleStrTillEx() { 1087 var ( 1088 haystack = `https://goframe.org/index.html?test=123456` 1089 needle = `?` 1090 result = gstr.StrTillEx(haystack, needle) 1091 ) 1092 fmt.Println(result) 1093 1094 // Output: 1095 // https://goframe.org/index.html 1096 } 1097 1098 // substr 1099 func ExampleSubStr() { 1100 var ( 1101 str = `1234567890` 1102 start = 0 1103 length = 4 1104 subStr = gstr.SubStr(str, start, length) 1105 ) 1106 fmt.Println(subStr) 1107 1108 // Output: 1109 // 1234 1110 } 1111 1112 func ExampleSubStrRune() { 1113 var ( 1114 str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` 1115 start = 14 1116 length = 3 1117 subStr = gstr.SubStrRune(str, start, length) 1118 ) 1119 fmt.Println(subStr) 1120 1121 // Output: 1122 // 高性能 1123 } 1124 1125 func ExampleStrLimit() { 1126 var ( 1127 str = `123456789` 1128 length = 3 1129 suffix = `...` 1130 result = gstr.StrLimit(str, length, suffix) 1131 ) 1132 fmt.Println(result) 1133 1134 // Output: 1135 // 123... 1136 } 1137 1138 func ExampleStrLimitRune() { 1139 var ( 1140 str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` 1141 length = 17 1142 suffix = "..." 1143 result = gstr.StrLimitRune(str, length, suffix) 1144 ) 1145 fmt.Println(result) 1146 1147 // Output: 1148 // GoFrame是一款模块化、高性能... 1149 } 1150 1151 func ExampleSubStrFrom() { 1152 var ( 1153 str = "我爱GoFrameGood" 1154 need = `爱` 1155 ) 1156 1157 fmt.Println(gstr.SubStrFrom(str, need)) 1158 1159 // Output: 1160 // 爱GoFrameGood 1161 } 1162 1163 func ExampleSubStrFromEx() { 1164 var ( 1165 str = "我爱GoFrameGood" 1166 need = `爱` 1167 ) 1168 1169 fmt.Println(gstr.SubStrFromEx(str, need)) 1170 1171 // Output: 1172 // GoFrameGood 1173 } 1174 1175 func ExampleSubStrFromR() { 1176 var ( 1177 str = "我爱GoFrameGood" 1178 need = `Go` 1179 ) 1180 1181 fmt.Println(gstr.SubStrFromR(str, need)) 1182 1183 // Output: 1184 // Good 1185 } 1186 1187 func ExampleSubStrFromREx() { 1188 var ( 1189 str = "我爱GoFrameGood" 1190 need = `Go` 1191 ) 1192 1193 fmt.Println(gstr.SubStrFromREx(str, need)) 1194 1195 // Output: 1196 // od 1197 } 1198 1199 // trim 1200 func ExampleTrim() { 1201 var ( 1202 str = `*Hello World*` 1203 characterMask = "*" 1204 result = gstr.Trim(str, characterMask) 1205 ) 1206 fmt.Println(result) 1207 1208 // Output: 1209 // Hello World 1210 } 1211 1212 func ExampleTrimStr() { 1213 var ( 1214 str = `Hello World` 1215 cut = "World" 1216 count = -1 1217 result = gstr.TrimStr(str, cut, count) 1218 ) 1219 fmt.Println(result) 1220 1221 // Output: 1222 // Hello 1223 } 1224 1225 func ExampleTrimLeft() { 1226 var ( 1227 str = `*Hello World*` 1228 characterMask = "*" 1229 result = gstr.TrimLeft(str, characterMask) 1230 ) 1231 fmt.Println(result) 1232 1233 // Output: 1234 // Hello World* 1235 } 1236 1237 func ExampleTrimLeftStr() { 1238 var ( 1239 str = `**Hello World**` 1240 cut = "*" 1241 count = 1 1242 result = gstr.TrimLeftStr(str, cut, count) 1243 ) 1244 fmt.Println(result) 1245 1246 // Output: 1247 // *Hello World** 1248 } 1249 1250 func ExampleTrimRight() { 1251 var ( 1252 str = `**Hello World**` 1253 characterMask = "*def" // []byte{"*", "d", "e", "f"} 1254 result = gstr.TrimRight(str, characterMask) 1255 ) 1256 fmt.Println(result) 1257 1258 // Output: 1259 // **Hello Worl 1260 } 1261 1262 func ExampleTrimRightStr() { 1263 var ( 1264 str = `Hello World!` 1265 cut = "!" 1266 count = -1 1267 result = gstr.TrimRightStr(str, cut, count) 1268 ) 1269 fmt.Println(result) 1270 1271 // Output: 1272 // Hello World 1273 } 1274 1275 func ExampleTrimAll() { 1276 var ( 1277 str = `*Hello World*` 1278 characterMask = "*" 1279 result = gstr.TrimAll(str, characterMask) 1280 ) 1281 fmt.Println(result) 1282 1283 // Output: 1284 // HelloWorld 1285 } 1286 1287 // version 1288 func ExampleCompareVersion() { 1289 fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) 1290 fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) 1291 fmt.Println(gstr.CompareVersion("2.8.beta", "2.8")) 1292 1293 // Output: 1294 // 1 1295 // -1 1296 // 0 1297 } 1298 1299 func ExampleCompareVersionGo() { 1300 fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) 1301 fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) 1302 fmt.Println(gstr.CompareVersionGo( 1303 "v0.0.2-20180626092158-b2ccc119800e", 1304 "v1.0.1-20190626092158-b2ccc519800e", 1305 )) 1306 1307 // Output: 1308 // 1 1309 // 1 1310 // -1 1311 }