UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Write a programme to replace the string without using replace command? This is my string. ..... s is replaced by th output is Thith ith my thtring

Asked by Last Modified  

Follow 0
Answer

Please enter your answer

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; import java.util.*; /** * * @author AP Edusoft */ public class Logic { /** ...
read more
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; import java.util.*; /** * * @author AP Edusoft */ public class Logic { /** * @param args the command line arguments */ public static void main(String[] args) { String originalString = "This is the cat"; String replacedString = replaceMethod(originalString, "the", "your"); System.out.println(replacedString); } static String replaceMethod(String str, String from, String to) { String[] arr = str.split(from); StringBuilder output = new StringBuilder(); int i = 0; for (; i < arr.length - 1; i++) output.append(arr[i]).append(to); output.append(arr[i]); if (str.substring(str.lastIndexOf(" ")).equalsIgnoreCase(" " + from)) output.append(to); return output.toString(); } } read less
Comments

Java Professional with 11+ Yrs of experience

Reverse String Method 1: 1. The user will input the string to be reversed. 2. First we will convert String to character array by using the built in java String class method toCharArray(). 3. Then , we will scan the string from end to start, and print the character one by one. Reverse String...
read more
Reverse String Method 1: 1. The user will input the string to be reversed. 2. First we will convert String to character array by using the built in java String class method toCharArray(). 3. Then , we will scan the string from end to start, and print the character one by one. Reverse String Method 2: 1. In the second method , we will use the built in reverse() method of the StringBuilder class ,. Note : String class does not have reverse() method . So we need to convert the input string to StringBuilder , which is achieved by using the append method of the StringBuilder. 2. After that print out the characters of the reversed string by scanning from the first till the last index. Reverse String Method 3: 1. Convert the input string into character array by using the toCharArray() built in method of the String Class . 2. In this method we will scan the character array from both sides , that is from the start index (left) as well as from last index(right) simultaneously. 3. Set the left index equal to 0 and right index equal to the length of the string -1. 4. Swap the characters of the start index scanning with the last index scanning one by one .After that increase the left index by 1 (left++) and decrease the right by 1 i.e (right--) to move on to the next characters in the character array . 5. Continue till left is less than or equal to the right . Reverse String Method 4: 1. Convert the input string into the character array by using toCharArray() built in method. reverse a string in java with example 2. Then add the characters of the array into the LinkedList object . We used LinkedList because it maintains the insertion order of the input values. 3. Java also has built in reverse() method for the Collections class . Since Collections class reverse() method takes a list object , to reverse the list , we will pass the LinkedList object which is a type of list of characters. 4. We will create the ListIterator object by using the listIterator() method on the LinkedList object. ListIterator object is used to iterate over the list. 5. ListIterator object will help us to iterate over the reversed list and print it one by one to the output screen. Reverse String Method 5 : 1. The last method is converting string into bytes . getBytes() method is used to convert the input string into bytes[]. 2. Then we will create a temporary byte[] of length equal to the length of the input string. 3. We will store the bytes(which we get by using getBytes() method) in reverse order into the temporary byte[] . read less
Comments

completeeducationhub.com web site you will get information

String str="This is my String"; String s=" "; for(int i=0;i
Comments

completeeducationhub.com web site you will get information

for(i=0;i
Comments

Java Technical Leader

Look at the program. If you face problem to understand this coding let me know. package com.test.scjp; public class StringReplacer { public String replaceMethod(String original, String toReplace, String replacedWith) { for(int j=0;j read more
Look at the program. If you face problem to understand this coding let me know. package com.test.scjp; public class StringReplacer { public String replaceMethod(String original, String toReplace, String replacedWith) { for(int j=0;jread less
Comments

Software Engineer

I have done this in javascript . i think u just only need to replace the var with string . var myStr = "Thith ith my thtring"; var newStr = ""; for(var i=0;i
Comments

Software Engineer

I have done this in javascript . i think u just only need to replace the var with string . var myStr = "Thith ith my thtring"; var newStr = ""; for(var i=0;i
Comments

Java Tutor

It can be done using split/substring method..
Comments

Computer science tutor

String str="This is my String"; StringBuffer buffer=new StringBuffer(); for(int i=0;i read more
String str="This is my String"; StringBuffer buffer=new StringBuffer(); for(int i=0;iread less
Comments

MCA, BIT Mesra, Ranchi

Apart from all the basic ways, this can be done with the help of pattern(regular expressions) which in-turn applies String class replaceAll method: public static String replaceString(String sourceStr, String replaceString, String replaceBy) { return Pattern.compile(replaceString).matcher(sourceStr).replaceAll(replaceBy); } public...
read more
Apart from all the basic ways, this can be done with the help of pattern(regular expressions) which in-turn applies String class replaceAll method: public static String replaceString(String sourceStr, String replaceString, String replaceBy) { return Pattern.compile(replaceString).matcher(sourceStr).replaceAll(replaceBy); } public static void main(String[] args) { System.out.println(replaceString("This is my string", "s", "th")); } Output: Thith ith my thtring read less
Comments

View 12 more Answers

Related Questions

What is reflection and what is its use in Java?
to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time
Rahul
0 0
7
Why does â??5/2 = 2â?? and not 2.5 in Python?
Due to dynamic Typing. use Numpy , and declare data type , you will get desired result
Srikanth
0 0
6
What are the various Java certifications?
The certifications are : 1) Oracle...
Rama
1 0
5
What are the difference between abstract class and Interface?
Abstraction : Hiding unnecessary details of object and shows only essential features of Object to communicate. abstract class : partially defined Object interface :Complete specification of Object class : Complete definition of Object
Ashish
How to find a good Java coaching centre?
Its all depends on the trainer and his expertise. I would suggest you to get feedback from the students who already joined.
Abirami
1 0
6

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

What Would Be Life Cycle Of A Fresher After Campus In An IT Company?
1. Basic Technical Training: Since freshers are not subject matter experts so gone through 3 - 6 months basic technical training within Organization. 2. Technical Assessment: HR sends freshers to various...

JAVA OOPs Concepts (Object-Oriented Programming System)
JAVA OOPs Concepts (Object-Oriented Programming System) It is primarily having below crucial points. Without below essential points, we will never be able to achieve OOPs in java, PHP, C#, etc. Now let...

Definition of Class and Object in Java
Object - Objects have states and behaviors. Example: A dog has states - colour, name, breed as well as behaviors - barking, eating, sleeping. An object is an instance of a class. Class - A class can be...

ClassNotFoundException vs NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError It is an exception and happens due to programmer’s mistake and can be recovered by updating the code. Thrown when an application tries...

Java : Compile-time Versus Runtime optimization
While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below : What...
S

Recommended Articles

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more