當前位置: 首頁>>代碼示例>>Golang>>正文


Golang strings.LastIndexFunc函數代碼示例

本文整理匯總了Golang中strings.LastIndexFunc函數的典型用法代碼示例。如果您正苦於以下問題:Golang LastIndexFunc函數的具體用法?Golang LastIndexFunc怎麽用?Golang LastIndexFunc使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了LastIndexFunc函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: LastIndexFunc

// LastIndexFunc returns the index into s of the last Unicode code point
// satisfying f(c) or -1 if none do
func LastIndexFunc(s string, f func(rune) bool) int {
	function := func(c rune) bool {
		return unicode.Is(unicode.Han, c)
	}

	fmt.Println(strings.LastIndexFunc("hello 世界", function))    // 10 一個漢字貌似占3個位置
	fmt.Println(strings.LastIndexFunc("hello world", function)) // -1
	return strings.LastIndexFunc(s, f)
}
開發者ID:upccup,項目名稱:cuplearn,代碼行數:11,代碼來源:stringsTest.go

示例2: Caller

// Caller report caller's position with file:function:line format
// depth means which caller, 0 means yourself, 1 means your caller
func Caller(depth int) string {
	_, file, line, _ := runtime.Caller(depth + 1)
	i := strings.LastIndexFunc(file, pathSepFunc)
	if i >= 0 {
		j := strings.LastIndexFunc(file[:i], pathSepFunc)
		if j >= 0 {
			i = j
		}
		file = file[i+1:]
	}
	return fmt.Sprintf("%s:%d", file, line)
}
開發者ID:sshitaime,項目名稱:gohper,代碼行數:14,代碼來源:runtime.go

示例3: main

func main() {
	as := ""
	for _, char := range []rune{'A', 0xE6, 0346, 330, '\xE6', '\u00E6'} {
		fmt.Printf("[0x%x '%c']", char, char)
		as += string(char)
	}
	fmt.Println(as)
	line := "Θ£ 123 Θ £ £Θ" //Θ and £  not only one char
	i := strings.Index(line, " ")
	i2 := strings.IndexFunc(line, unicode.IsSpace)
	firstword := line[:i]
	j := strings.LastIndex(line, " ")
	j2 := strings.LastIndexFunc(line, unicode.IsSpace)
	lastword := line[j+1:]
	item, size := utf8.DecodeRuneInString(line[j+1:]) //返回第一個字符(可能超過一般的ascii碼)和字節數
	fmt.Println(i, ",", i2, ",", firstword, ",", j, ",", j2, lastword, item, size)
	bs:="{\"error\":\"thumbnail's arg is out of range: 650001456812x\"}"
	i=strings.Index(bs,":")
	j=strings.LastIndex(bs,"}")
	fmt.Println(bs[i+2:j-1])
	

	var iss []int{1,2,3}
	fmt.Println()
}
開發者ID:needkane,項目名稱:qiniu,代碼行數:25,代碼來源:testString.go

示例4: NewTurtleConfig

func NewTurtleConfig(syslogLog *syslog.Writer) (*TurtleConfig, error) {

	//DEBUG
	syslogLog.Notice("    [CONFIG] BEGIN.")

	cmdPath := os.Args[0]
	cmdPathLastSlash := strings.LastIndexFunc(cmdPath, func(c rune) bool {
		return '/' == c
	})

	cmdDirPath := cmdPath[0:cmdPathLastSlash]

	//DEBUG
	syslogLog.Notice(fmt.Sprintf("    [CONFIG] command path: [%v]", cmdPath))
	syslogLog.Notice(fmt.Sprintf("    [CONFIG] command path last slash: [%v]", cmdPathLastSlash))
	syslogLog.Notice(fmt.Sprintf("    [CONFIG] command dir path: [%v]", cmdDirPath))

	confRelativePath := "turtledq.ini"
	confPath := cmdDirPath + "/" + confRelativePath

	//DEBUG
	syslogLog.Notice(fmt.Sprintf("    [CONFIG] settings file relative path: [%v]", confRelativePath))
	syslogLog.Notice(fmt.Sprintf("    [CONFIG] settings file absolute path: [%v]", confPath))

	return NewTurtleConfigFromFile(syslogLog, confPath)
}
開發者ID:reiver,項目名稱:turtledq,代碼行數:26,代碼來源:config.go

示例5: ParseTime

// ParseTime parses the time interval from s, and returns it
// as nanoseconds, if it is representable in seconds (with
// isNanoSeconds true), or sample count (with isNanoSeconds false)
// if not.
func ParseTime(s string) (t Time, err os.Error) {
	endi := strings.LastIndexFunc(s, isDigit) + 1
	if endi == 0 {
		return Time{}, os.NewError("invalid number")
	}
	number, suffix := s[0:endi], s[endi:]
	var mult int64
	switch suffix {
	case "s", "":
		mult = 1e9
	case "ms":
		mult = 1e6
	case "us":
		mult = 1e3
	case "ns":
		mult = 1
	case "x": // samples
		mult = 1
	}
	// use exact arithmetic if we can
	d, err := strconv.Atoi64(number)
	if err != nil {
		f, err := strconv.Atof64(number)
		if err != nil {
			return Time{}, err
		}
		d = int64(f * float64(mult))
	} else {
		d *= mult
	}
	return Time{d, suffix != "x"}, nil
}
開發者ID:zxpbenson,項目名稱:rog-go,代碼行數:36,代碼來源:w_time.go

示例6: main

func main() {
	phrase := "naïve\u2028🚀"
	fmt.Printf("string: %s\n", phrase)
	fmt.Println("len:", len(phrase),
		"RuneCountInString:", utf8.RuneCountInString(phrase))
	fmt.Println("index rune     char bytes")
	for index, char := range phrase {
		fmt.Printf(" %2d   %-8U  %c    % X\n",
			index, char, char, []byte(string(char)))
	}
	last_rune, length := utf8.DecodeLastRuneInString(phrase)
	fmt.Printf("last rune: 0x%x length: %d\n", last_rune, length)
	fmt.Printf("length of string: %d length of []rune: %d\n",
		len(phrase), len([]rune(phrase)))
	fmt.Printf("line separator: %[1]U 0x%[1]x '%[1]c' %s\n", '\u2028', string('\u2028'))
	fmt.Printf("paragraph separator: %[1]U 0x%[1]x '%[1]c' %s\n", '\u2029', string('\u2029'))

	line := "rå tørt\u2028vær"
	i := strings.IndexFunc(line, unicode.IsSpace)
	firstWord := line[:i]
	j := strings.LastIndexFunc(line, unicode.IsSpace)
	_, size := utf8.DecodeRuneInString(line[j:])
	lastWord := line[j+size:]
	fmt.Println("size of space:", size)
	fmt.Println(firstWord, lastWord)
}
開發者ID:pto,項目名稱:go-book,代碼行數:26,代碼來源:phrase.go

示例7: Word

// Word returns a string with the word of the view's internal buffer
// at the position corresponding to the point (x, y).
func (v *View) Word(x, y int) (string, error) {
	x, y, err := v.realPosition(x, y)
	if err != nil {
		return "", err
	}

	if x < 0 || y < 0 || y >= len(v.lines) || x >= len(v.lines[y]) {
		return "", errors.New("invalid point")
	}

	str := lineType(v.lines[y]).String()

	nl := strings.LastIndexFunc(str[:x], indexFunc)
	if nl == -1 {
		nl = 0
	} else {
		nl = nl + 1
	}
	nr := strings.IndexFunc(str[x:], indexFunc)
	if nr == -1 {
		nr = len(str)
	} else {
		nr = nr + x
	}
	return string(str[nl:nr]), nil
}
開發者ID:robert-butts,項目名稱:slackterm,代碼行數:28,代碼來源:view.go

示例8: main

func main() {
	fmt.Println(strings.Index("Hello, world!", "He"))  // 0: He가 맨 처음에 있으므로 0
	fmt.Println(strings.Index("Hello, world!", "wor")) // 7: wor가 8번째에 있으므로 7
	fmt.Println(strings.Index("Hello, world!", "ow"))  // -1: ow는 없으므로 -1

	fmt.Println(strings.IndexAny("Hello, world!", "eo")) // 1: e가 2번째에 있으므로 1
	fmt.Println(strings.IndexAny("Hello, world!", "f"))  // -1: f는 없으므로 -1

	var c byte
	c = 'd'
	fmt.Println(strings.IndexByte("Hello, world!", c)) // 11: d가 12번째에 있으므로 11
	c = 'f'
	fmt.Println(strings.IndexByte("Hello, world!", c)) // -1: f는 없으므로 -1

	var r rune
	r = '언'
	fmt.Println(strings.IndexRune("고 언어", r)) // 4: "언"이 시작되는 인덱스가 4

	f := func(r rune) bool {
		return unicode.Is(unicode.Hangul, r) // r이 한글 유니코드이면 true를 리턴
	}
	fmt.Println(strings.IndexFunc("Go 언어", f))       // 3: 한글이 4번째부터 시작하므로 3
	fmt.Println(strings.IndexFunc("Go Language", f)) // -1: 한글이 없으므로 -1

	fmt.Println(strings.LastIndex("Hello Hello Hello, world!", "Hello"))
	// 12: 마지막 Hello가 13번째에 있으므로 12

	fmt.Println(strings.LastIndexAny("Hello, world", "ol")) // 10: 마지막 l이 11번째에 있으므로 10

	fmt.Println(strings.LastIndexFunc("Go 언어 안녕", f)) // 13: 마지막 한글인 '녕'이 시작되는 인덱스가 13
}
開發者ID:jemoonkim,項目名稱:golangbook,代碼行數:31,代碼來源:strings_Index.go

示例9: moveDotLeftWord

func moveDotLeftWord(ed *Editor) {
	if ed.dot == 0 {
		return
	}
	space := strings.LastIndexFunc(
		strings.TrimRightFunc(ed.line[:ed.dot], unicode.IsSpace),
		unicode.IsSpace) + 1
	ed.dot = space
}
開發者ID:yonglehou,項目名稱:elvish,代碼行數:9,代碼來源:insert.go

示例10: killWordLeft

// NOTE(xiaq): A word is now defined as a series of non-whitespace chars.
func killWordLeft(ed *Editor) {
	if ed.dot == 0 {
	}
	space := strings.LastIndexFunc(
		strings.TrimRightFunc(ed.line[:ed.dot], unicode.IsSpace),
		unicode.IsSpace) + 1
	ed.line = ed.line[:space] + ed.line[ed.dot:]
	ed.dot = space
}
開發者ID:rathinaganesh,項目名稱:elvish,代碼行數:10,代碼來源:builtins.go

示例11: lastWord

func lastWord(s string) string {

	idx := strings.LastIndexFunc(s, func(c rune) bool {
		return c >= 'A' && c <= 'Z'
	})
	if idx >= 0 {
		return s[idx:]
	}
	return ""
}
開發者ID:ttthzy,項目名稱:qlang,代碼行數:10,代碼來源:interpret2.go

示例12: killWordLeft

// NOTE(xiaq): A word is now defined as a series of non-whitespace chars.
func killWordLeft(ed *Editor, k Key) *leReturn {
	if ed.dot == 0 {
		return nil
	}
	space := strings.LastIndexFunc(
		strings.TrimRightFunc(ed.line[:ed.dot], unicode.IsSpace),
		unicode.IsSpace) + 1
	ed.line = ed.line[:space] + ed.line[ed.dot:]
	ed.dot = space
	return nil
}
開發者ID:horryq,項目名稱:elvish,代碼行數:12,代碼來源:builtins.go

示例13: TestIndexFunc

func TestIndexFunc(t *testing.T) {
	for _, tc := range indexFuncTests {
		first := strings.IndexFunc(tc.s, tc.f)
		if first != tc.first {
			t.Errorf("strings.IndexFunc(%q, %s) = %d; want %d", tc.s, tc.fname, first, tc.first)
		}
		last := strings.LastIndexFunc(tc.s, tc.f)
		if last != tc.last {
			t.Errorf("strings.LastIndexFunc(%q, %s) = %d; want %d", tc.s, tc.fname, last, tc.last)
		}
	}
}
開發者ID:skey,項目名稱:go_examples,代碼行數:12,代碼來源:strings_test.go

示例14: stringSuffixIndexFunc

//  Returns the index i of the longest terminal substring s[i:] such that f
//  returns true for all runes in s[i:]. Returns -1 if there is no such i.
func stringSuffixIndexFunc(s string, f func(c rune) bool) (i int) {
	var hasSuffix bool
	i = strings.LastIndexFunc(s, func(c rune) (done bool) {
		if done = !f(c); !hasSuffix {
			hasSuffix = !done
		}
		return
	})
	if i++; !hasSuffix {
		i = -1
	}
	return
}
開發者ID:bmatsuo,項目名稱:goline,代碼行數:15,代碼來源:strings.go

示例15: splitAtNumber

// splitAtNumber splits given string into prefix and numeric suffix.
// If no numeric suffix exists, full original string is returned as
// prefix with -1 as a suffix.
func splitAtNumber(str string) (string, int) {
	i := strings.LastIndexFunc(str, func(r rune) bool {
		return !unicode.IsDigit(r)
	}) + 1
	if i == len(str) {
		// no numeric suffix
		return str, -1
	}
	n, err := strconv.Atoi(str[i:])
	if err != nil {
		panic(fmt.Sprintf("parsing number %v: %v", str[i:], err)) // should never happen
	}
	return str[:i], n
}
開發者ID:imoapps,項目名稱:juju,代碼行數:17,代碼來源:naturalsort.go


注:本文中的strings.LastIndexFunc函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。