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

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

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

Data Integration Specialist

use recursion or do-while
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

Need more R&D.
Comments

SeleniumAutomation_Java_Interview_Prepartion_MOCK_Interviews

public void replaceCharc(){ String str="This is my string. ..... s"; char charToBeReplaced='s'; char charToBeReplaceWith='t'; System.out.println("String before replacemnet is "+str); char arrStr=str.toCharArray(); String replacedString=""; for(char c : arrStr){ if(c==charToBeReplaced){ replacedString+=charToBeReplaceWith; }else{ replacedString+=c; } } System.out.println("String...
read more
public void replaceCharc(){ String str="This is my string. ..... s"; char charToBeReplaced='s'; char charToBeReplaceWith='t'; System.out.println("String before replacemnet is "+str); char [] arrStr=str.toCharArray(); String replacedString=""; for(char c : arrStr){ if(c==charToBeReplaced){ replacedString+=charToBeReplaceWith; }else{ replacedString+=c; } } System.out.println("String after replacement is "+replacedString); } read less
Comments

View 12 more Answers

Related Questions

Is Java a language or a technology?
java is both language and technology. Java platform serves as technology.
Sammeyka
0 0
5
Does Java support pointers?
Yes and no. . The pointer model is of course supported by Java. But it has become much easier to the developer to handle it. Because Java exposes the concept of pointer in terms of reference variables...
Santosh
0 0
7
Which programming language should one use for large scale machine learning, Java or C++?
Java but preferred language now is R or Scala or Python .
Rayaan
0 0
5
Can Anybody explain Internal code of HashMap?
Now we can synchronize map also by using collections. Synchronize method need to pass map Object as parameter...
Ramakanth
best source to learn java from?
Main source if information on Java is oracle documentation. But it takes your time to read through, a lot more than you expect. Better prefer a trainer who can give what you need.
Himanshu

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

Ask a Question

Related Lessons

Create Immutable Class
Snippet of an Immutable Class: package com.stringhr; /*Declare the class as 'final'. This would prevent any other class from extending it and hence from overriding any method from it which could modify...

doWhile example in Java
public class doWhilePracticleEx { public void test() { } public static void main(String args) { String q1 = "Who is PM of India?"; String a1 = "Sonia Gandhi"; String a2 = "Rahul Gandhi"; String a3...
S

Sarthak C.

0 0
0

Be prepared to get trained--init
Before starting the training,students must be mentally prepared for acceptance of new knowledge. Students must attend training with open minded forgetting the position they are working.This will help...
S

Smartnub Softsolutions

0 0
0

CoreJAVA
Core Java Training High Level Course Content Trained by Java Architect 1. Core Java Programming Introduction of Java 2. Data types and Operators 3. Control Flow statements 4. OOPS and its application...
A

Introduction to Course Content
Video about what we are going to learn throughout the Java Training Session .

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