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

Is it good to go for a Java coaching center and learn Java (with advanced Java) to improve my programming skills?
yes, Please check the Institute reviews and discuss with the tutor, what topics were covered in class. Ask about the practicals session and how many hours/duration of the course.
Gerda
0 0
5
Is Java a compiler or Interpreter?
Java is a programming language and computing platform first released by Sun Microsystems in 1995. A Java compiler is a compiler for the Java programming language. A JVM interprets bytecode and...
Dr Rakesh
What is the difference between abstraction and encapsulation?
Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example...
Neval
What are the best websites to learn java as well as get a certificate from them?
To learn basic java i suggest javatpoint is good for knowledge as well as concept but for certification refer book
Abirami
0 0
7
What is selection sort in java?
Selection sort is a sorting algorithm. You can implement this algorithm in any language. The basic idea is to select the smallest element of the array and placing it to the head of array. This step needs...
Snehashis

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

Ask a Question

Related Lessons

COMPILATION AND INTERPRETATION
Compilation and Interpertation Process javac (compiler) java(interpreter)high level code - > compile - > bytecode - > interperted - > machine code Bytecode...

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...

Interview Tip : Q1) Why Strings are immutable in java ? What happen if it was mutable in java?
As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program. The answer to this...

Demand of java in industry
Java is one of the widest use language in the industry all around the world As in software market more than 80% software, platform developed using java. Java comes in three flavours:- j2se for window based...

Prashant Tomer

0 0
0

Android : Application Launch time improvements.
For any standard android application, below 3 components play important roles to show 1st interface to user, so that he/she can interact with the app. 1. Custom Application class : Intialize the components...

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