Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:28:39+00:00 2026-06-12T10:28:39+00:00

I have a string in Chinese: x = 你好 I’d like to loop through

  • 0

I have a string in Chinese:

x = "你好"

I’d like to loop through it and do something with each character in it, something like:

for i, len := 0, len(x); i < len; i++ {
    foo( x[i] ) // do sth.
}

I found that len(x) would return 6 instead of 2, after Google I found the method RuneCountInString which would return the real length of the string, but I still don’t know how to loop to make x[i] get the right character, x[0] == '你' for example..

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:28:42+00:00Added an answer on June 12, 2026 at 10:28 am

    Use range.

    x = "你好"
    for _, c := range x {
        // do something with c
    }
    

    If you want random-access, you’ll need to use code unit indexes rather than character indexes. Fortunately, there is no good reason to need character indexes, so code unit indexes are fine.

    Most languages have the exact same problem. For example, Java and C# use UTF-16, which is also a variable-length encoding (but some people pretend it isn’t).

    See the UTF-8 Manifesto for more information about why Go uses UTF-8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string that look like Array that fetched from other webservice like this
I have a string that is a sentence, written in chinese. This contains chinese
I have some chinese charactors encoded like this: String b = \\u91d1\\u5143\\u6bd4\\u8054\\u6210\\u957f\\u52a8\\u529b; How can
I have a query string passed in through an HTTP request that has this
I have a string containing mostly chinese characters, like so: string sentence = 我想找到从夏洛特飞往拉斯维加斯,让站在圣路易斯;
I have a file with chinese content that I need to parse. Each post
I have string that displays UTF-8 encoded characters, and I want to convert it
I have string like this : <input name=my_name id=my_id value=my_value class=my_class /> I would
I have string like \LESSING\root\cimv2:Win32_UserAccount.Domain=LESSING,Name=Admin How to convert it to LESSING\Admin using Framework?
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.