Access the string contained in a slice

王林
Release: 2024-02-10 20:00:19
forward
1154 people have browsed it

Access the string contained in a slice

php editor Xiaoxin is here to introduce how to access the strings contained in the slice. In PHP, slicing refers to the operation of intercepting a part of characters from a string. By accessing the strings in the slice, we can get the required data or perform other operations. When using slicing, we need to specify the starting position and ending position to get the corresponding string. Mastering the use of slices will bring great convenience to our development work. Next, let us learn in detail how to access the string contained in the slice!

Question content

I am doing some coding exercises to understand go better. The given exercise instructs me to create a program that accepts user input as follows:

  • The first line specifies how many strings will be provided as input on separate lines
  • Each of the next n lines is a single string

I want to output the characters corresponding to the even and odd indexes of each string, separated by spaces, and with each string on a separate line.

Input example:

2
foo_bar
fizz_buzz
Copy after login

should output:

fobr o_a
fz_uz izbz
Copy after login

But accessing the string slice in my program returns an empty string:

package main

import (
    "fmt"
)

func main() {
    // read an integer describing how many strings will be input
    var num_strings int
    fmt.scan(&num_strings)

    // create a slice of strings to hold provided strings
    strings := make([]string, num_strings)

    // add provided strings to slice
    for i := 0; i < num_strings; i++ {
        var temp string
        fmt.scan(&temp)
        strings = append(strings, temp)
    }

    // check that strings have been appended
    fmt.println("strings:", strings)

    // check that strings can be accessed
    for i := 0; i < num_strings; i++ {
        fmt.println(i, strings[i]) // only i prints, not strings[i]
    }

    // loop over all strings
    for i := 0; i < num_strings; i++ {

        // if string index is even print the char
        for index, val := range strings[i] {
            if index%2 == 0 {
                fmt.print(val)
            }
        }

        fmt.print(" ")

        // if string index is odd print the char
        for index, val := range strings[i] {
            if index%2 != 0 {
                fmt.print(val)
            }
        }

        // newline for next string
        fmt.print("\n")
    }
}
Copy after login
2
foo_bar
fizz_buzz
Strings: [  foo_bar fizz_buzz]
0 
1
Copy after login

Workaround

Because when you make strings slice you are creating a capacity and length is a slice of n. So when you append to it, you increase the length of the slice:

Change this code:

// create a slice of strings to hold provided strings
strings := make([]string, num_strings)

// add provided strings to slice
for i := 0; i < num_strings; i++ {
  var temp string
  fmt.scan(&temp)
  strings = append(strings, temp)
}
Copy after login

arrive:

// create a slice of strings to hold provided strings
strings := []{}

// add provided strings to slice
for i := 0; i < num_strings; i++ {
  var temp string
  fmt.scan(&temp)
  strings = append(strings, temp)
}
Copy after login

or

// create a slice of strings to hold provided strings
strings := make([]string, num_strings)

// add provided strings to slice
for i := 0; i < num_strings; i++ {
  var temp string
  fmt.Scan(&temp)
  strings[i] = temp
}
Copy after login

You should be on your best behavior.

The above is the detailed content of Access the string contained in a slice. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!