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") 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









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...   Syntax: REPLACE( String_Text, String_FROM, String...'); Output :   RoseIndIa.net We can also apply this Replace function
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
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
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...;green"> <c:out value="${fn:replace(string,find
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
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
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 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
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.... There is also direct method to replace all spaces by calling one of String method
doubt this
doubt this  what is the use of "this" and "super" keyword
Replace
Replace   Hi, I have two text files in two diff locatons.The text file like below using java program 1)a.txt (adsbygoogle = window.adsbygoogle || []).push({}); 1 abc bangalore 2 def adfsdf 3
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
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
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
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
Replace Character in String
in a given string. To replace a character with the given character in sting first... C:\replace>java Replace Your String before repalce rajesh raju raja... Replace Character in String      
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...:replace(string,find,replaceWith)}"/> </font>... a string after replacing the given sub string with specified sub string. This takes
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
MySQL String Function
MySQL String Function      ...'); -> 100 BIN (N) The BIN string function return a string...) The CHAR_LENGTH(str) function returns String str lengths that is measured
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
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
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
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: ADS_TO_REPLACE_1 General Format
JavaScript String Function
JavaScript String Function Almost every language provides special attention towards string and string manipulation, JavaScript is no exception. It provides many built-in functions to manipulate a string.  Java String Function
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
PHP String Function
PHP String Function: A string is nothing but a series of characters. Most easiest way to specify the string is to enclose it in single quotes (' '.... We can freely print these all. PHP String Function Example: <?phpADS
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
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
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
Java replace all non alphanumeric
|| []).push({});   Hi, You can use the replaceAll() function of String... show how to replace all non alphanumeric characters present in a Java String...Java replace all non alphanumeric  Hi, In Java how to replace all
String replaceAll() Method
that word. This method will replace all the words in a string as shown in the output... String replaceAll() Method       In this program you will learn how to replace all the words
Java 11 String lines function example to split it in lines
Java 11 String lines function - How to split String into lines in Java 11? In this tutorial we are going to explain use the use of new function which.... In this version of Java (JDK 11) String class introduces a new method
Java 11 String lines function example to split it in lines
Java 11 String lines function - How to split String into lines in Java 11? In this tutorial we are going to explain use the use of new function which.... In this version of Java (JDK 11) String class introduces a new method
python replace newline with space
of the string to performance replace operation. Here is code example: string...python replace newline with space  Hi, I want to replace newline character \n With space in python. How to replace newline with space in python
python replace newline with space
of the string to performance replace operation. Here is code example: string...python replace newline with space  Hi, I want to replace newline character \n With space in python. How to replace newline with space in python
Difference between translate and replace
of string. this work for entire word and pattern,. Translate replace the one...Difference between translate and replace   hiii, What is the difference between translate and replace?   hello,ADS_TO_REPLACE_1 Replace
Validation doubt
have got that and implemented in my code but i have a doubt in that. As we try to put string values its not allowing to do tht it gives us message its right... think i am able to tell u what i want to and u have got whats my doubt. plz give
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function
String replaceAll in java
String replaceAll in java In this section you will learn about replaceAll() method  in java, This will replace each of the sub string  with the given replacement. This method will return the resulting string. Syntax

Ads