Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User pavanragi's Avatar
    Join Date
    Jul 2012
    Location
    India
    Posts
    103
    Hii
    I have a requirement to remove HTML Special Characters and replace the Special Character with the respective value from a String in j2me java........

    For Example:

    I have a String like this:

    Federation of A Chambers of Commerce & Industry Awards for the year 2010-11. Speaking on the occasion,
    He said, "About 54 percent of the population is youth aged below 25 years. We have to use their energy and
    intelligence for development of the state as well as the country.The youth trained will also be absorbed by
    companies.’"

    I want to replace " with "
    I want to replace &nbsp with space
    I want to replace ’ with ’
    I want to replace & with &

    Like this etc......................

    The text is dynamic that i will get From different Links

    Like this etc...................... Any Help........

  2. #2
    Nokia Developer Expert Villep's Avatar
    Join Date
    Jun 2012
    Posts
    55
    Hello,
    It is possible. You need a method to replace a string with another string. Using String.indexOf and String.substring it is possible to create such.

    Sample which would replace " with "
    Code:
    String text = ""Single quote";
    int index = text.indexOf(""");
    if (index != -1) {
       text = text.substring(0, index) + "\"" + text.substring(index + """.length());
    }
    Br,
    Villep

  3. #3
    Nokia Developer Expert skalogir's Avatar
    Join Date
    Aug 2011
    Posts
    547
    Hi pavanragi,

    Would something like this cover your case?
    Code:
    protected void startApp() {
    		String in = "Federation of A Chambers of Commerce & Industry Awards for the year 2010-11. Speaking on the occasion," +
                          "He said, "About 54 percent of the population is youth aged below 25 years. We have to use their energy and" +
                          "intelligence for development of the state as well as the country.The youth trained will also be absorbed by" +
                          "companies.’" ";
    		
    		String out = replace(in, """, "\"");
    		out = replace(out, "&nbsp", " ");
    		out = replace(out, "’", "’");
    		out = replace(out, "&", "&");
    		
    		
    		System.out.println(out);
    	
    	}
    	
    	protected String replace(String s, String from, String to) {
    		int i = s.indexOf(from);
                    //if the special character is found at least once in the text
    		if(i >= 0) {
                            //until all instances of the special character are replaced
    			while(i >= 0) {
                                    //if the special character is found at the beginning of the text
    				if(i == 0)
    					s = to + s.substring(1, s.length());
    				//if the special character is found after the beginning of the text
                                    else
    					s = s.substring(0, i) + to + s.substring(i + from.length(), s.length());
            		        //look for new replacements from the current cursor position until the end of the text
    				i = s.indexOf(from, i + from.length());
    			}
    		}
    		
    		return s;
        }
    Last edited by skalogir; 2012-07-27 at 09:17.

  4. #4
    Registered User pavanragi's Avatar
    Join Date
    Jul 2012
    Location
    India
    Posts
    103
    Hi Villep and skalogir

    Thanks For reply,

    your sample codes below are simply perfect ,But in real time ,How many Html Special Characters Will come from Different Html WEbPages i dont know,
    I mean for How many Special Characters ,I have to replace again and again?,It is Dynamic?,I think My Question is correct?



    Quote Originally Posted by Villep View Post
    Hello,
    It is possible. You need a method to replace a string with another string. Using String.indexOf and String.substring it is possible to create such.

    Sample which would replace " with "
    Code:
    String text = ""Single quote";
    int index = text.indexOf(""");
    if (index != -1) {
       text = text.substring(0, index) + "\"" + text.substring(index + """.length());
    }
    Br,
    Villep
    Last edited by pavanragi; 2012-07-27 at 08:28.

  5. #5
    Nokia Developer Expert skalogir's Avatar
    Join Date
    Aug 2011
    Posts
    547
    Hi pavanragi,

    The attached code, includes a while loop that identifies all instances of a given special character and replaces them all with a single call to the replace() method. I have added some comments in the code.

  6. #6
    Registered User pavanragi's Avatar
    Join Date
    Jul 2012
    Location
    India
    Posts
    103
    Hii skalogir

    Ya ,I got it...Thanks....


    Quote Originally Posted by skalogir View Post
    Hi pavanragi,

    The attached code, includes a while loop that identifies all instances of a given special character and replaces them all with a single call to the replace() method.

Similar Threads

  1. Special characters?
    By rsiudak in forum Symbian C++
    Replies: 2
    Last Post: 2010-10-08, 14:33
  2. Special Characters
    By nitant_flashlite in forum [Archived] Flash Lite on Nokia Devices
    Replies: 1
    Last Post: 2010-02-06, 16:29
  3. Replies: 1
    Last Post: 2009-05-16, 10:30
  4. Special characters in MMP?
    By smui in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 2
    Last Post: 2008-03-14, 09:13
  5. Special Characters
    By singh_gundeep in forum Symbian User Interface
    Replies: 1
    Last Post: 2008-02-19, 08:23

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved