当前位置:网站首页>[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
}边栏推荐
- Zero basic learning canoe panel (7) -- input/output box
- JMeter interface test steps - Installation Tutorial - script recording - concurrent test
- Simply understand MODBUS function code and partition
- Zero basic learning canoe panel (3) -- static text, group box, picture box
- LoRa无线技术与LoRaWAN网关模块的区别
- Zero basis learning canoe panel (5) -- change the value of the variable, and the control image also changes. What's going on?
- MySQL查询字段匹配某个规则的记录
- Development and course of Bluetooth Technology
- 这个应该是全网最全的接口测试工具之postman
- Reptiles and counter crawls: an endless battle
猜你喜欢
![[micro service] eureka+ribbon realizes registration center and load balancing](/img/a7/57e28ce146270524774fd5d3304d96.png)
[micro service] eureka+ribbon realizes registration center and load balancing

零基础学习CANoe Panel(3)—— 静态控件(Static Text , Group Box ,Picture Box)

Zero basic learning canoe panel (9) -- combobox

零基础学习CANoe Panel(6)—— 开关/显示控件(Switch/Indicator)

JMeter接口测试步骤-安装教程-脚本录制-并发测试
![[white hat talks about web security] Chapter 2 browser security](/img/dc/3640da863f9e5e527c9de27e55e99c.png)
[white hat talks about web security] Chapter 2 browser security

Robot Framework官方教程(一)入门

零基础学习CANoe Panel(7)—— 文件选择(PathDiaglog)

【直播报名】Location Cache 模块浅析及 OCP 监控、报警详解

RS485 communication OSI model network layer
随机推荐
浅析拉格朗日乘数法及其对偶问题
[interview: Basics 02: bubble sort]
TwinCAT3各版本下载路径
Logic of automatic reasoning 06 -- predicate calculus
Development and course of Bluetooth Technology
Kubernetes Foundation
[micro service] eureka+ribbon realizes registration center and load balancing
Nodejs installation tutorial
1184. Distance between bus stops: simple simulation problem
JS tree structure, find out the parent set of each layer it belongs to according to the ID of the inner layer
MySQL paging
[FPGA]: IP core -- rapid IO
对话ACE第四期:分布式数据库未来发展的挑战和机遇
数据可视化-《白蛇2:青蛇劫起》(1)
[interview: Basics 01: integer binary search]
Modbus RTU通讯协议详解与实例演示
[FPGA]: IP core ibert
pip更新命令
Redis cache settings, similar to putifabsent function
Redistribution distributed lock types