Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
How to Count the Number of Repeated Characters in a Golang String?
Next article icon

How to Count the Number of Repeated Characters in a Golang String?

Last Updated : 26 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go string, you can count some specific Unicode code points or the number of non-overlapping instances of costr (Repeated Characters) in the string with the help of Count() function. This function returns a value which represents the total number of the given string or Unicode code points present in the string. It is defined under the strings package so, you have to import strings package in your program for accessing Count function. Syntax:
func Count(str, costr string) int
Here, str is the original string and costr is a string which we want to count. If the value of costr is empty, then this function returns 1 + the number of Unicode code points in str. Example: C
// Go program to illustrate how to // count the elements of the string package main  import (     "fmt"     "strings" )  // Main function func main() {      // Creating and initializing the strings     str1 := "Welcome to the online portal of GeeksforGeeks"     str2 := "My dog name is Dollar"     str3 := "I like to play Ludo"      // Displaying strings     fmt.Println("String 1: ", str1)     fmt.Println("String 2: ", str2)     fmt.Println("String 3: ", str3)      // Counting the elements of the strings     // Using Count() function     res1 := strings.Count(str1, "o")     res2 := strings.Count(str2, "do")      // Here, it also counts white spaces     res3 := strings.Count(str3, "")     res4 := strings.Count("GeeksforGeeks, geeks", "as")      // Displaying the result     fmt.Println("\nResult 1: ", res1)     fmt.Println("Result 2: ", res2)     fmt.Println("Result 3: ", res3)     fmt.Println("Result 4: ", res4)  } 
Output:
  String 1:  Welcome to the online portal of GeeksforGeeks  String 2:  My dog name is Dollar  String 3:  I like to play Ludo    Result 1:  6  Result 2:  1  Result 3:  20  Result 4:  0  

Next Article
How to Count the Number of Repeated Characters in a Golang String?

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang
  • Golang-String

Similar Reads

    How to Replace Characters in Golang String?
    In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to replace characters in the given string usin
    4 min read
    Counting number of repeating words in a Golang String
    Given a string, the task is to count the number of words being repeated in that particular string in Golang. Example: Input: s = "She is mother of my mother." Output: She = 1 is = 1 mother = 2 of = 1 my = 1 To count the number of repeating words, first, the string is taken as input and then strings.
    2 min read
    How to Generate Random String/Characters in Golang?
    We might want to generate random strings or even sets of characters to perform some operations or add certain string-related functionality into an application. We can randomly get a character from a set of characters, randomize the order of characters of a given string or generate a random string. W
    6 min read
    Repeating a String for Specific Number of Times in Golang
    In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You are allowed to repeat a string of for a specific number of times with the
    3 min read
    How to find the Length of Channel, Pointer, Slice, String and Map in Golang?
    In Golang, len function is used to find the length of a channel, pointer, slice, string, and map. Channel: In Go language, a channel is a medium through which a goroutine communicates with another goroutine and this communication is lock-free. C // Go program to illustrate // how to find the length
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences