Home  >  Article  >  Backend Development  >  Access the string contained in a slice

Access the string contained in a slice

王林
王林forward
2024-02-10 20:00:191155browse

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

should output:

fobr o_a
fz_uz izbz

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")
    }
}
2
foo_bar
fizz_buzz
Strings: [  foo_bar fizz_buzz]
0 
1

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)
}

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)
}

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
}

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete