Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with String replace and CharSequence

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with String replace and CharSequence

    Im trying to replace a "illegalCharacter" in a string, but ive had no success with doing it with a CharSequence, i can get it to detect the char that i want to remove but i cant replace it.

    IllegalCharacters[] = null;
    CharSequence IllegalChar = null;
     
    //Defining illegal characters \/:*?"<>|
        IllegalCharacters = new char[9];
        IllegalCharacters[0] = 92;
        IllegalCharacters[1] = 47;
        IllegalCharacters[2] = 58;
        IllegalCharacters[3] = 42;
        IllegalCharacters[4] = 63;
        IllegalCharacters[5] = 34;
        IllegalCharacters[6] = 60;
        IllegalCharacters[7] = 62;
        IllegalCharacters[8] = 124;
     
    IllegalChar = java.nio.CharBuffer.wrap(IllegalCharacters);
    System.out.println(sub);
    boolean BoolContainsIllegal = sub.contains(IllegalChar);
            if(BoolContainsIllegal = true){
             sub = sub.replace(IllegalChar, "_");
             System.out.println(sub);
            }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with String replace and CharSequence

    You want to replace with?????
    Suppose, you have illegal character at index 6.
    And you want to replace it with say 000.
    IllegalCharacters[6]=000;

    Is this, what you was trying to ask??? May be i didn't get the clear understanding of what you are looking for.

  3. #3

    Default Re: Problem with String replace and CharSequence

    What is sub? Where does it comes from? What are your errors?
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with String replace and CharSequence

    Sorry about that i might have given you too little info but heres an explenation.

    Sub contains a string which suppousedly contains an illegal character.
    Im defining all the characters, (in a char Array called "IllegalCharacters"),that are illegal for this string to have ex -> :
    Im putting all the illegal characters into a charBuffer called "IllegalChar", im then searching the string for a illegal character if it exists in the string it retruns true.

    Say the variable Sub contains = "Hi "
    Then the search will retrun true and then the replac should happen.
    Up to this point theres no problems, but when i execute the statement:
    sub = sub.replace(IllegalChar, "_");
    Noting actually happens.... In other words the character " : " that should have been replaced with "_" hanst been replaced, so the string output is the same as the one i ran before the actual "if" statement

    I get no errors from running this.

    Hope this explains it better!

Similar Threads

  1. [SOLVED] Replace String Method? Noobie :/
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 18th, 2011, 10:58 AM
  2. Why is comparison of objects of Class implementing CharSequence undefined?
    By rsameera in forum Object Oriented Programming
    Replies: 1
    Last Post: September 4th, 2011, 02:30 PM
  3. Error with contains and CharSequence.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2011, 09:19 AM
  4. [SOLVED] Replace Phrase in String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2010, 12:10 PM
  5. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM