Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners String doubt replace function
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ciba kiran
String doubt replace function
2 Answer(s)      3 years and 11 months ago
Posted in : Java Beginners


What is the output and why of below :

String s1 = "Hello"; if(s1.replace('H','H')== "Hello") System.out.println("yes"); else System.out.println("No"); if(s1.replace("H", "H")== "Hello") System.out.println("yes"); else System.out.println("No");


Advertisement
View Answers

February 11, 2011 at 4:17 PM


A string cannot be compared with == operator. It is always compared with equals() function.

Compare string:

class Replace 
{
    public static void main(String[] args) 
    {
        String s1 = "Hello";
        if(s1.replace('H','H').equals( "Hello")){
            System.out.println("yes"); 
        }
        else {
            System.out.println("No");
        }
        if(s1.replace("H", "H").equals("Hello")){
            System.out.println("yes");
        }
        else{
            System.out.println("No");
        }
    }
}


February 11, 2011 at 4:29 PM


Thanks for the prompt reply. But, I understand the correct function for comparison is".equals()" and not "==". I wish to understand what is the difference when we use function replace(char old, car new) and replace("stringOld", "stringNew") which causes the above below stated output(obtained after running the code)? yes No


Related Tutorials/Questions & Answers:
String doubt replace function
String doubt replace function  What is the output and why of below : String s1 = "Hello"; if(s1.replace('H','H')== "Hello") System.out.println("yes"); else System.out.println("No"); if(s1.replace("H", "H")== "Hello
MySql find and replace string
MySql find and replace string  How to find and replace string or special characters in MySql & PHP?   Find and Replace String PHP $result = preg_replace('/\band\b/i', 'foo', $subject
Advertisements
Replace String in PHP - PHP
Replace String in PHP  What is the fastest way of replacing string in PHP Code?  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/php/phpbasics/PHP-Array-Replace.html Thanks
MySQL Replace
; MySQL Replace function finds and replaces the occurrences of specified string...: REPLACE( String_Text, String_FROM, String_TO); where String_Text... apply this Replace function on the tables data while inserting or updating
C Replace String
C Replace String       In this section, you will learn how to replace a string in C.  Here a function pointer replace is created in which variables st, orig, repl
Replace a String with another using "fn:replace" tag of JSTL
Replace a String with another using <fn:replace> tag of JSTL In this Section, we will replace a String with another string using <fn:replace> tag of JSTL. This tag returns a string after replacing the given sub string
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
Replace
Replace   Hi, I have two text files in two diff locatons.The text file like below using java program 1)a.txt 1 abc bangalore 2 def adfsdf 3 ghij asdfdsad 2)b.txt A B C Now i want output is like below
String Function Program
String Function Program  The string function program is just an application of the string manipulation functions in JAVA. The program is designed to count the number of one-letter word, two-letter word, and three-letter words
Java String : Replace All
Java String : Replace All This section defines how to replace all character... : You can replace all specific character in a given string with another character... method to replace all spaces by calling one of String method replaceAll("
doubt this
doubt this  what is the use of "this" and "super" keyword
how to find a dynamic string in a string & how to replace tgat wth with another string - JSP-Servlet
how to find a dynamic string in a string & how to replace tgat wth... from a excel sheet specified column values and i have one string... these values into a string ,use that string to send a mail ,these 2,3,4 ,5
Doubt
Doubt  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details
Replace Character in String
in a given string. To replace a character with the given character in sting first...:\replace>java Replace Your String before repalce rajesh raju raja rahul ray rani ram... Replace Character in String      
Doubt
User request form  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details... LANGUAGE="JavaScript"> function getParams() { var idx = document.URL.indexOf
Doubt
How to load page  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit details... LANGUAGE="JavaScript"> function getParams() { var idx = document.URL.indexOf
Doubt
Submit and process form  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how to submit... LANGUAGE="JavaScript"> function getParams() { var idx = document.URL.indexOf
Doubt
load next page after submitting  how to submit the details and how to go the next page after submitting.please clarify my doubt I don't know how...; <script LANGUAGE="JavaScript"> function getParams() { var idx
doubt
doubt  what is the difference between public static void main(String[] args) public static void main(String args
Use of <fn:replace(String, String, String)> Tag of JSTL
Use of <fn:replace(String, String, String)> Tag of JSTL...; color="green"> <c:out value="${fn:replace(string,find... : Suppose in user wants to replace all 'x' with character 'a' in string "
ereg_replace() and eregi_replace().
ereg_replace() and eregi_replace().  What is the difference between eregreplace() and eregireplace()?   Hi friends, ereg_replace() is case sensitive like "Bharat"cannot be treated as "bharat"and eregi_replace
PHP Array Replace
PHP Array Replace Sometimes we have to replace array values with given values, PHP provides array_replace() function to replace the values. array_replace... description of array_replace() is:  General Format array array
find and replace in java
find and replace in java  Need 'find and replace' function in Java to find special keyword in XMl such as @,#,!,%..and replace with their corresponding entities
Use of string-length() function in XPath
Use of string-length() function in XPath       In this section we will discuss about string-length() function in XPath. string-length() function returns the number
PHP Array Replace Recursive
PHP Array Replace Recursive: PHP provides array_replace_recursive() function to replace elements from passed arrays into the first array recursively. array_replace_recursive() replaces the values of first array with the same values from
Replacing String using Regularexpression
Replacing String using Regularexpression       This Example describe the way to replace a String using Regularexpression. The steps involved in replacing a String are described
MySQL String Function
, to_str) The REPLACE (str, from_str, to_str) function returns the String str... MySQL String Function      ... BIN (N) The BIN string function return a string value
Java replace all non alphanumeric
() function of String class in Java. We will use the regular expression "[^a-zA-Z0-9...Java replace all non alphanumeric  Hi, In Java how to replace all non alphanumeric characters with blank? Share me the best code for java replace
how to write a function to print for finding the longest palindrome in the given string
how to write a function to print for finding the longest palindrome in the given string  how to write a function to print for finding the longest palindrome in the given string
How ro convert char into string using recursive function in c#??
How ro convert char into string using recursive function in c#??  This coding is for java...but i want this in c# How ro convert char into string using recursive function in c#?? char [] ch={'H','E','L','L','o'}; TO ans
Difference between translate and replace
Difference between translate and replace   hiii, What is the difference between translate and replace?   hello, Replace replace every instence of character with character sting by the given charator of string
Advertisements
 

 

 

DMCA.com