How to split a string by space and newline in Golang? [duplicate] - go

This question already has answers here:
Split a string on whitespace in Go?
(4 answers)
Closed 10 months ago.
I'm a beginner in Golang and I want to split a multiline text terminated by the EOF indicator, and I want to do it by the space and the presence of new lines ( since the user is gonna press "enter" a lot ).
Any idea of how?

Use strings.Fields
words := strings.Fields(someString)
See example in The Go Playground

Related

line break or simplify or make long payload data line by line without error [duplicate]

This question already has answers here:
how to split long lines for fmt.sprintf
(5 answers)
Best practice for long string literals in Go
(5 answers)
indentation style for multi-line string literals
(3 answers)
How do you write multiline strings in Go?
(11 answers)
How to format a multiline string literal [closed]
(2 answers)
Closed 6 months ago.
I had a variable payload, it is too long here just a part.
payload := strings.NewReader("json=%7B%22rent_content_layout%22%3A%22Original%22%2C%22start_verylargetext_enabled%22%3Afalse%2C%22auto_large_mode")
is any way to make the one line var data to multi line or make it human readable using golang, for bash or shell we use \n, is any trick to line break it using golang
Simply do the string concatenation with the + operator and break the string into small parts.
payload := strings.NewReader(
"json=%7B%22" +
"rent_content_layout%22%3A%22" +
"Original%22%2C%22" +
"start_verylargetext_enabled%22%3A" +
"false%2C%22auto_large_mode")

Yaml - how to break very long string [duplicate]

This question already has answers here:
How do I break a string in YAML over multiple lines?
(9 answers)
Closed 4 years ago.
I have an entry in my yaml file that looks like this
my_key:['short string', 'thisisaverylongstringthatcontains.,specialcharacterssoI havetousequotes,andI wanttobreakintomultiplelines']
My very long string can't contain spaces, and I am worried if I simply use newline, it will get converted to space.
What is the cleanest, simplest way to break down that second string across multiple lines for easier readability and convenience?
key : ['short string', "this is a very long string
that I want to break into
multiple lines"]
Have you tried just inserting a newline? This is valid YAML.
If you don't want spaces, add \ to the end of each line.
key : ['short string', "this is a very long string\
that I want to break into\
multiple lines"]

Enter Key code in MessageBox object (Visual Studio) [duplicate]

This question already has answers here:
C#: New line and tab characters in strings
(6 answers)
Closed 9 years ago.
Enter in MessageBox:
My Message:
Warning:
Do you want to continue?
Code:
MessageBox.Show("Warning: {ENTER(?)} Do you want to continue?");
Rather than the {ENTER(?)} to send enter code and message divide to two line.
Please try
MessageBox.Show("Warning:\nDo you want to continue?");
\n stands for NewLine. You can use Environment.NewLine, too. Please check out this Question
C#: New line and tab characters in strings
it might contain other helpful answers.

Search and replace string in unix bash regex [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Closed 9 years ago.
Does anyone have an idea on how to search and replace in a string? Let's say for example I have a string
string=".blah http://google.com.ph/tabs/1.5.8 setup https://yahoo.com.ph/root/blah"
I want to search for version 1.5.8 and then replace it with 1.5.9. How do i do it in bash?
instring="version 1.5.8"
outstring=${instring//1.5.8/1.5.9}

what is the best way to remove the last n characters of a string (in Ruby)? [duplicate]

This question already has answers here:
Ruby, remove last N characters from a string?
(13 answers)
Closed 5 years ago.
in Ruby,
I just want to get rid of the last n characters of a string,
but the following doesn't work
"string"[0,-3]
nor
"string".slice(0, -3)
I'd like a clean method, not anything like
"string".chop.chop.chop
it may be trivial, please anyone teach me! thanks!
You can use ranges.
"string"[0..-4]
You could use a regex with gsub ...
"string".gsub( /.{3}$/, '' )
If you add an ! to slice it will destructively remove the last n characters without having to assign it to another variable:
my_string.slice!(my_string.length-3,my_string.length)
compared to:
new = my_string.slice(0..-4)

Resources