String doubt replace function

Ads
 

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") System.out.println("yes"); else System.out.println("No");

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

Ads









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
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
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
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
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
java string replace all non digits
java string replace all non digits  Hi, I am doing some processing... except the digits. How to replace all non digits in a string with some character... in combination with the replaceAll function of String. Following regular
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
JavaScript replaceAll function to replacing all occurrences of a string
occurrences of a string. How make JavaScript replaceAll function to replacing all... with replacement. You can find many examples at How to replace all occurrences of a string...JavaScript replaceAll function to replacing all occurrences of a string 
JavaScript replaceAll function to replacing all occurrences of a string
occurrences of a string. How make JavaScript replaceAll function to replacing all... with replacement. You can find many examples at How to replace all occurrences of a string...JavaScript replaceAll function to replacing all occurrences of a string 
JavaScript replaceAll function to replacing all occurrences of a string
occurrences of a string. How make JavaScript replaceAll function to replacing all... with replacement. You can find many examples at How to replace all occurrences of a string...JavaScript replaceAll function to replacing all occurrences of a string 
How to replace all occurrences of a string in JavaScript?
in JavaScript Today we are going to learn different ways to replace string in JavaScript. There are many ways to replace all occurrences of a string in JavaScript... of replacing string in JavaScript. 1. Replace all occurrences with regular expression
How to replace all occurrences of a string in JavaScript?
in JavaScript Today we are going to learn different ways to replace string in JavaScript. There are many ways to replace all occurrences of a string in JavaScript... of replacing string in JavaScript. 1. Replace all occurrences with regular expression
How to replace all occurrences of a string in JavaScript?
in JavaScript Today we are going to learn different ways to replace string in JavaScript. There are many ways to replace all occurrences of a string in JavaScript... of replacing string in JavaScript. 1. Replace all occurrences with regular expression
doubt this
doubt this  what is the use of "this" and "super" keyword
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
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
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
doubt  what is the difference between public static void main(String[] args) public static void main(String args
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      
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
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
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 "
Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents.
Write a function that reads a string of digits, possibly preceded... a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents. Be sure that your function
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
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
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source

Ads