当前位置:网站首页>[golang] golang实现截取字符串函数SubStr
[golang] golang实现截取字符串函数SubStr
2022-07-24 11:05:00 【陶士涵】
golang中没有很多其他语言中的截取字符串函数
现在单独定义一个函数来处理
// 截取字符串,支持多字节字符
// start:起始下标,负数从从尾部开始,最后一个为-1
// length:截取长度,负数表示截取到末尾
func SubStr(str string, start int, length int) (result string) {
s := []rune(str)
total := len(s)
if total == 0 {
return
}
// 允许从尾部开始计算
if start < 0 {
start = total + start
if start < 0 {
return
}
}
if start > total {
return
}
// 到末尾
if length < 0 {
length = total
}
end := start + length
if end > total {
result = string(s[start:])
} else {
result = string(s[start:end])
}
return
}边栏推荐
- 自动推理的逻辑06--谓词演算
- [white hat talks about web security] Chapter 1 my security world view
- UVM - two way communication
- MySQL queries tables and fields according to comments
- 自学软件测试天赋异禀——不是盖的
- Idea background image set
- pip更新命令
- 简单使用 MySQL 索引
- Web salted fish self rescue strategy -- typescript classes are not as difficult as you think
- 变频器的工作原理和功能应用
猜你喜欢
![[FPGA]: IP core - multiplier](/img/c5/141ba8e5291454bb33225c7e28567d.png)
[FPGA]: IP core - multiplier

Five network IO models
![[attack and defense world web] difficulty five-star 15 point advanced question: ics-07](/img/97/555a76be9e96629fd7379ce8612a3b.png)
[attack and defense world web] difficulty five-star 15 point advanced question: ics-07

JMeter interface test steps - Installation Tutorial - script recording - concurrent test
![[FPGA]: IP core --divider (divider)](/img/bc/d8b7638e236c468ba23c8afc7ab70e.png)
[FPGA]: IP core --divider (divider)

Zero basic learning canoe panel (8) -- hex/text editor

Princeton chendanqi: how to make the "big model" smaller

Use Modelsim to independently simulate Altera and Xilinx IP cores

Publish local image to private library

High speed ADC test experience
随机推荐
自学软件测试天赋异禀——不是盖的
Cookie sessionstorage localstorage differences
[FPGA]: IP core ibert
Logic of automatic reasoning 06 -- predicate calculus
[FPGA]: IP core -- rapid IO
[white hat talks about web security] Chapter 2 browser security
1184. Distance between bus stops: simple simulation problem
Docker installs 3 master and 3 slave redis clusters
Five application scenarios of Bluetooth module
FastCGI运行原理及php-fpm参数配置
Nodejs installation tutorial
数据可视化-《白蛇2:青蛇劫起》(1)
Detailed explanation of the implementation process of redistribution watchdog
Five network IO models
爬虫与反爬:一场无休止之战
[ISE] development process plus bit, burning of MCS files
JS tree structure, find out the parent set of each layer it belongs to according to the ID of the inner layer
Zero basic learning canoe panel (7) -- input/output box
Reptiles and counter crawls: an endless battle
LDR6028充电OTG直播线直播声卡音频转接器最具性价比方案