当前位置:网站首页>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: xxxxxThere are multiple line breaks between each line , And what we want most of the time is this :
xxxxxxx
yyyyyy: xxxxx
yyyyyy: xxxxx
yyyyyy: xxxxxThat 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 32among 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 !
边栏推荐
- HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
- Is it safe to open an online stock account? Who knows
- System Verilog - function and task
- QQ love talk candy love talk content acquisition and storage
- Leetcode123 timing of buying and selling stocks III
- Basic knowledge of pointer
- semget No space left on device
- Boost listening port server
- Judging the number of leap years from 1 to N years
- Summary of regularization methods
猜你喜欢

QT set process startup and self startup

How to make GIF animation online? Try this GIF online production tool

QT source code online view

Review of arrays and pointers triggered by a topic

Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)

Source code analysis of synergetics and ntyco
![[paper notes] rethinking and improving relative position encoding for vision transformer](/img/6b/8b1c192e0ce715789465fa9ccaabfc.jpg)
[paper notes] rethinking and improving relative position encoding for vision transformer

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

From 408 to independent proposition, 211 to postgraduate entrance examination of Guizhou University

Pytorch distributed test pit summary
随机推荐
Advertising effect cluster analysis (kmeans)
Gif动画怎么在线制作?快试试这款gif在线制作工具
If multiple signals point to the same slot function, you want to know which signal is triggered.
Using R language in jupyter notebook
Qcodeeditor - QT based code editor
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
Why should the coroutine be set to non blocking IO
QT set process startup and self startup
Data feature analysis skills - correlation test
(translation) json-rpc 2.0 specification (Chinese version)
Study notes of cmake
5 connection modes of QT signal slot
Semaphore function
Flexible layout (display:flex;) Attribute details
Vs2019 scanf error
Afterword of Parl intensive learning 7-day punch in camp
QT database connection
2022年广东高考分数线出炉,一个几家欢喜几家愁
One question per day,
JS select all exercise