CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 1999
    Location
    Germany (south)
    Posts
    147

    Is there a function which returns the number of a special character in a String?

    Hi all,

    is there a function which returns the number of "x" from the String "ABCxxDEFxGxHIJxxx"? (==> for example)

    ==> should return 7

    Thanx all,
    Andi





  2. #2
    Join Date
    Mar 2000
    Location
    Tamil Nadu, India
    Posts
    12

    Re: Is there a function which returns the number of a special character in a String?

    I dont think there is a function to get the count, but a function like countPattern in the below program should help.

    public class PatternCount {
    public static void main(String[] args) {
    String str = "ABCxxDEFxGxHIJxxx";
    System.out.println(countPattern(str,"x"));
    }
    public static int countPattern(String str,String pattern) {
    int count = 0;
    for(int i=-1;(i=str.indexOf(pattern,i+1)) >= 0;count++);
    return count;
    }
    }



    rums1976


  3. #3
    Join Date
    Jun 1999
    Location
    Germany (south)
    Posts
    147

    Re: Is there a function which returns the number of a special character in a String?

    Hi there,

    thank you for your answer.

    Unfortunatelly I am missing lot´s of functions in JAVA I had while I was programming with MFC - anyway, thank you very much!
    Andi




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured