当前位置:网站首页>Go language modifies / removes multiple line breaks in strings
Go language modifies / removes multiple line breaks in strings
2022-06-25 15:15:00 【_ Qilixiang】
Up like water , Great simplicity .
Catalog
The problem background
For example, some strings are so long :
xxxxxxx
yyyyyy: xxxxx
yyyyyy: xxxxx
yyyyyy: xxxxx
There are multiple line breaks between each line , And what we want most of the time is this :
xxxxxxx
yyyyyy: xxxxx
yyyyyy: xxxxx
yyyyyy: xxxxx
That is to say, it is OK to keep a newline between the contents !
This is based on common functions and \n It may be impossible to judge , I met them and analyzed them , Write an algorithm to share with you , I hope we can solve similar problems as soon as possible .
solve
If the common string function cannot be used to judge the newline , You can't use string To operate the , First to []rune have a look , For example, the four newlines in my string are as follows :
10 32 10 32 10 32 10 32
among 10 It's line breaking ,32 Is a space , And keep the first newline 、 The left end of each line of content needs to be aligned after removing redundant line breaks , So the algorithm is as follows :
func TrimWrap(src string) (dist string) {
if len(src) == 0 {
return
}
r, distR := []rune(src), []rune{}
var m, k, n int32
for i := 0; i < len(r); i++ {
if i == 1 && len(r) == 2{
if r[i] == 10 && r[i-1] == 10 {
continue
}
}
if i >= 2 {
m, k, n = r[i-2], r[i-1], r[i]
}
if m == 10 && n == 10 && k == 32 {
continue
}
if m == 32 && n == 32 && k == 10 {
continue
}
distR = append(distR, r[i])
}
dist = string(distR)
return
}
If your need is only for multiple line breaks without worrying about the impact of spaces , Just change the logic a little . If you don't need to keep line breaks at all, it's easier .
If it helps you , Welcome to leave your footprints !
边栏推荐
- A deformation problem of Hanoi Tower
- Bessie's weight problem [01 backpack]
- QT loading third-party library basic operation
- Yolov3 spp Darknet version to caffemodel and then to OM model
- In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
- HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
- Sampling method and descriptive statistical function in R language
- Generation method and usage of coredump
- Qt: Pro project file
- Basic knowledge of pointer
猜你喜欢
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
15 -- 最接近原点的 K 个点
[C language] implementation of magic square array (the most complete)
Position (5 ways)
JS select all exercise
Dynamic memory allocation
Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language
Master XSS completely from 0 to 1
有哪个瞬间让你觉得这个世界出bug了?
(1) Introduction
随机推荐
Arithmetic operations and expressions
How to package rpm
Bessie's weight problem [01 backpack]
How to make GIF animation online? Try this GIF online production tool
Qt: Pro project file
QT set process startup and self startup
55 specific ways to improve program design (2)
Introduction to flexible array
One question per day, a classic simulation question
Shared memory synchronous encapsulation
QT database connection deletion
Leetcode123 timing of buying and selling stocks III
QT loading third-party library basic operation
One question per day,
QT excel table read / write library - qtxlsx
Is it safe to open a stock account online?
QT article outline
The difference between sizeof and strlen
Character encoding minutes
搭建极简GB28181 网守和网关服务器,建立AI推理和3d服务场景,然后开源代码(一)