github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/string.go (about)

     1  package common
     2  
     3  import "strings"
     4  
     5  func SubstringAfter(s string, substr string) string {
     6  	index := strings.Index(s, substr)
     7  	if index == -1 {
     8  		return s
     9  	}
    10  	return s[index+len(substr):]
    11  }
    12  
    13  func SubstringAfterLast(s string, substr string) string {
    14  	index := strings.LastIndex(s, substr)
    15  	if index == -1 {
    16  		return s
    17  	}
    18  	return s[index+len(substr):]
    19  }
    20  
    21  func SubstringBefore(s string, substr string) string {
    22  	index := strings.Index(s, substr)
    23  	if index == -1 {
    24  		return s
    25  	}
    26  	return s[:index]
    27  }
    28  
    29  func SubstringBeforeLast(s string, substr string) string {
    30  	index := strings.LastIndex(s, substr)
    31  	if index == -1 {
    32  		return s
    33  	}
    34  	return s[:index]
    35  }
    36  
    37  func SubstringBetween(s string, after string, before string) string {
    38  	return SubstringBefore(SubstringAfter(s, after), before)
    39  }