Вы находитесь на странице: 1из 5

String replace()

The java string replace() method returns a string replacing all the old char or CharSequence
to new char or CharSequence.

public class ReplaceExample1{

public static void main(String args[]){

String s1="abcd aabc Aabc";

String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'

System.out.println(replaceString);

}}

Output:

ebcd eebc Aebc


Ex:

public class ReplaceExample2{

public static void main(String args[]){

String s1="He is reading a book";

String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was"

System.out.println(replaceString);

}}

Output:
He was reading a book

String trim()
The java string trim() method eliminates leading and trailing spaces.

Ex:

public class StringTrimExample{

public static void main(String args[]){

String s1=" hello string ";

System.out.println(s1+" this is without trim method ");//without trim()


System.out.println(s1.trim()+"this is trim method");//with trim()

}}

Output:

hello string this is without trim method


hello stringthis is trim method

changing the case of characters:

toLowerCase()
The java string toLowerCase() - it converts all characters of the string into lower case letter.

public class StringLowerExample{

public static void main(String args[]){

String s1="STRING is Hello";

String s1lower=s1.toLowerCase();

System.out.println(s1lower);

}}

Output:

string is hello

toUpperCase()
The java string toUpperCase() -it converts all characters of the string into upper case letter.

toUpperCase() method example

public class StringUpperExample{

public static void main(String args[]){

String s1="hello string";

String s1upper=s1.toUpperCase();

System.out.println(s1upper);

}}
Output:

HELLO STRING

valueOf()
The java string valueOf() method converts different types of values into string. By the help
of string valueOf() method, you can convert int to string, long to string, boolean to string,
character to string, float to string, double to string, object to string and char array to string.

public class StringValueOfExample5 {

public static void main(String[] args) {

boolean b1=true;

byte b2=11;

short sh = 12;

int i = 13;

long l = 14L;

float f = 15.5f;

double d = 16.5d;

char chr[]={'j','a','v','a'};

String s1 = String.valueOf(b1);

String s2 = String.valueOf(b2);

String s3 = String.valueOf(sh);

String s4 = String.valueOf(i);

String s5 = String.valueOf(l);

String s6 = String.valueOf(f);

String s7 = String.valueOf(d);

String s8 = String.valueOf(chr);

String s9 = String.valueOf(obj);

System.out.println(s1);

System.out.println(s2);
System.out.println(s3);

System.out.println(s4);

System.out.println(s5);

System.out.println(s6);

System.out.println(s7);

System.out.println(s8);

true
11
12
13
14
15.5
16.5
Java

Ex: Write a Java Program to sort the strings in ascending order


import java.util.Scanner;
public class Alphabetical_Order
{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
}
}
Output:
Enter number of names you want to enter:5
Enter all the names:
ece
cse
eee
civil
mech
Names in Sorted Order:civil,cse,ece,eee,mech

StringBuffer class
StringBuffer class is used to create a mutable string object i.e its state can be changed after it is
created. It represents growable and writable character sequence.
StringBuffer class is used when we have to make lot of modifications to our string.

 StringBuffer() creates an empty string buffer and reserves room for 16 characters.
 stringBuffer(int size) creates an empty string and takes an integer argument to set
capacity of the buffer.

class Rextester {

public static void main(String args[])

String str = "Mahatma";

str.concat("Gandhi");

System.out.println(str); // Output: study

StringBuffer strB = new StringBuffer("Mahatma");

strB.append("Gandhi");

System.out.println(strB); // Output: studytonight

Output:

Mahatma
MahatmaGandhi

Вам также может понравиться