Home Answers Viewqa Java-Beginners String doubt replace function

 
 


ciba kiran
String doubt replace function
2 Answer(s)      2 years and 4 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");

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 Pages:
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 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
DOUBT ?
DOUBT ?  in c++ member function of a class must be public
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
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
Programming - Replace method
Java: Programming - Replace method Change all occurrences in a string Write a method that replaces all occurrences of one string with another. The predefined Java function replace may not be used for this -- you have to write
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
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
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
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
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
Login validation doubt
="java.sql.*"%> Username: Password: 2)check.jsp: <% String user=request.getParameter("user"); String pass=request.getParameter("pass"); if(user.equals...;html> <script> function validate(){ var username=document.form.user.value
JSP basic doubt
JSP basic doubt  String user = request.getParameter("username...)check.jsp: <% String user=request.getParameter("user"); String pass...; function validate(){ var username=document.form.user.value; var password
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
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
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
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
big doubt
;% /* String path = request.getContextPath(); String basePath... d = new Date(); long l = d.getTime(); String query = "select... + "and broadcast_end_time<=" + l; String msg = ""; try
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 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("
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
string
string  a java program to replace all matching substrings
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
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
doubt this
doubt this  what is the use of "this" and "super" keyword
Replace broken Image using JQuery
Replace broken Image using JQuery  Sir My web page contains a lot... code : $(window).bind('load', function() { $('img').each(function...', 'missing.jpg'); } }); }) This code will replace the broken image
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      
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 "
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 about JMS selector
doubt about JMS selector  Hi! , i have a little problem with message selector in JMS api. I'am try develop a request and response with JMS queue... selector the message is ever null String selector = "JMSCorrelationID
doubt about JMS selector
doubt about JMS selector  Hi! , i have a little problem with message selector in JMS api. I'am try develop a request and response with JMS queue... the reply using the selector following selector the message is ever null String
how to replace the values - Development process
how to replace the values  i have arraylist containing "or","c1... ArrayList2 { public static void main(String[] args) { ArrayList al = new...); String temp = ""; for(int i=0;i
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
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 in combobox in flex - XML
doubt in combobox in flex  hi, i have doudt in combobox in flex which is a combination of mxml and actioscript . In my project i has... access to combo box item name as string. As per your query i coded an example
string
string  program for String reverse and replace in c,c++,java e.g.Input:10,20,hello output:hello;20;10   Hi Friend, Try the following java...(String[] args) { String array[]=new String[5]; Scanner input
doubt on synchronized block in java
doubt on synchronized block in java  Hi ! some people are feeling.... I think you got my doubt. I request you to clarify my doubt based on below... synchronizedblock1 { public static void main(String args[] ) { table t=new table
Doubt in servlets - JSP-Servlet
Doubt in servlets  Good Evening sir/madam, I want to add data dynamically for that which methods i have to use to retrieve...(); String name=req.getParameter("name"); String address=req.getParameter
string
string  a java program using string function to input any string... ArrangeStringAlphabetically { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter string
string
string  a java program using string function that computes your initials from your full name and display them   Please visit the following links: http://www.roseindia.net/tutorial/java/core/printInitials.html http
doubt in ejb3 - EJB
doubt in ejb3  hi i am new to ejb3 .i have written simple code which... { String Username; Integer Password; public UserEntityBean( String username... = password; // this.author = author; } public String getUsername
string
string  a java program using string function and access modifiers to automate the insurance company which will have to manage the information about policy holders Data Members: 1)Policy No 2)Name of policy holder 3)Address 4
doubt - Date Calendar
doubt about retrieve dates from Database  Retrieve dates from...:</b></td><%Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "register"; String driver
doubt - Java Beginners
c; private String driver = "com.mysql.jdbc.Driver"; private String user = "amar"; private String pass = "amar123"; private String url = "jdbc:mysql://192.168.10.211:3306/"; private String db = "amar"; public void
Please clarify my doubt
A { synchronized void foo(B b) { String name... A.last()"); } } class B { synchronized void bar(A a) { String...(a); System.out.println("Back in other thread"); } public static void main(String