Você está na página 1de 3

Reverse a string in java without using any java API.

Ex: Ram is a Good Boy will give the output yoB dooG a si maR

package com.akshaya.Sep27Example;
public class RevStringWithoutAPI {
public static String str="Ram is a Good Boy";
public static void main(String[] args) {
char[] str1=new char[str.length()];
for(int i=str.length()-1,j=0;i>=0;i--,j++){
str1[j]=str.charAt(i);
}
System.out.println(str1);
}
}
O/P: yoB dooG a si maR
Reverse String word in the reverse word without using any java API
Ex: Hello World will give output World Hello

package com.akshaya.Sep27Example;
public class RevStringWordWithoutAPI {
public static void main(String[] args) {
{
String str="Java 2 Career";
//It will work if the separation of word in string is a blank space
String arr[]=str.split(" ");
// It will work if the separation of word in string is any special character
// String arr[]=str.split(Pattern.quote("."));
System.out.print("Reversed String of "+str+":");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
for(int j=arr.length-1;j>=0;j--){
System.out.print(arr[j] );
System.out.print(" ");
}
}
}}
O/P: Java
2
Career
Career 2 Java

3. Java String to Integer Examples:


// Convert using Integer.parseInt():
Signature: public static int parseInt(String s) throws NumberFormatException

Ex:
String numberAsString = "1234";
int number = Integer.parseInt(numberAsString);
System.out.println("The number is: " + number);
// Convert using Integer.valueOf()
Signature: public static Integer valueOf(String s) throws
NumberFormatException
String numberAsString = "1234";
int number = Integer.valueOf(numberAsString);
System.out.println("The number is: " + number);
//Convert using new Integer(String).intValue()
String numberAsString = "1234";
Integer intObject = new Integer(numberAsString);
int number = intObject.intValue();
//Convert using DecimalFormat
String numberAsString = "1234";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
int number = decimalFormat.parse(numberAsString).intValue();
System.out.println("The number is: " + number);//1234
} catch (ParseException e) {
System.out.println(numberAsString + " is not a valid number.");
}
//Convert with special radix
String numberAsString = "11111111";
int number1 = Integer.valueOf(numberAsString, 2);
int number2 = Integer.parseInt(numberAsString, 2);
System.out.println(number1);//255
System.out.println(number2);//255
String numberAsString = "377";
int number1 = Integer.valueOf(numberAsString, 8);
int number2 = Integer.parseInt(numberAsString, 8);
System.out.println(number1);//255
System.out.println(number2);//255
String numberAsString = "ff";
int number1 = Integer.valueOf(numberAsString, 16);
int number2 = Integer.parseInt(numberAsString, 16);
System.out.println(number1);//255
System.out.println(number2);//255
4.
ABCDEFGHIJ
BCDEFGHIJ
CDEFGHIJ
DEFGHIJ
EFGHIJ
FGHIJ

GHIJ
HIJ
IJ
J
public class SubstringTest {
public static void main(String[] args) {
String testString = "ABCDEFGHIJ";
System.out.println(testString.substring(0));
System.out.println(testString.substring(1));
System.out.println(testString.substring(2));
System.out.println(testString.substring(3));
System.out.println(testString.substring(4));
System.out.println(testString.substring(5));
System.out.println(testString.substring(6));
System.out.println(testString.substring(7));
System.out.println(testString.substring(8));
System.out.println(testString.substring(9));
}
}
5.Search for a String in an array of strings.