Oct 29

String lastIndexOfExample in java

[code lang=”java”]public class StringLastIndexOfExample {
public static void main(String[] args) {

String string="Hello World";
int lastIndex;
lastIndex=string.lastIndexOf(‘l’);

System.out.println("last index of character l is "+lastIndex);
lastIndex=string.lastIndexOf("ll");
System.out.println("last index of String ll is "+lastIndex);
lastIndex=string.lastIndexOf(‘l’,5);
System.out.println("last index of character l starting from 5th position"+lastIndex);

}

}[/code]

Output
last index of character l is 9
last index of String ll is 2
last index of character l starting from 5th position3

Oct 28

How to remove last character from string in java?

Example to remove last character from String in java

[code lang=”java”]public class StripLastCharacterExample {

public static void main(String[] args) {

String string="HELLO WORLD";

if(string!=null && !string.isEmpty())
{
System.out.println("Before removing last character from string "+string);
string=string.substring(0,string.length()-1);
System.out.println("After removing last character from string "+string);
}
}

}

[/code]

Output :
Before removing last character from string–> HELLO WORLD
After  removing last character from string–> HELLO WORL

Oct 21

String getChars() Example

String getChars() method example:

[code lang=”java”]public class StringGetCharsExample {
public static void main(String[] args) {

String string="Hello World";

char [] charArray=new char[4];
// 0 -from which position the string should be copied
//4-ending character of original string
//charArray-characters are copied to this string.
//0-characters are copied starting from this position.

string.getChars(0, 4, charArray, 0);
System.out.println("copied string");
System.out.println(charArray);

}

}

[/code]

Output

copied string
Hello

Oct 19

String equals IgnoreCase example in java

[code lang=”java”]public static void main(String[] args) {
boolean value;

String str = "HELLO";
String string1 = "Hello";
String string2 = "hello";

value = str.equalsIgnoreCase(string1);
System.out.println(str + " and " + string1 + " are equal " + value);

value = str.equalsIgnoreCase(string2);
System.out.println(str + " and " + string2 + " are equal " + value);
}
[/code]

Output :
HELLO and Hello are equal true
HELLO and hello are equal true

Oct 08

How to copy only some characters in String in java?

String copyOffset example in java

[code lang=”java”]
public class StringCopyOfWithOffSet {
public static void main(String[] args) {
char[] str={‘j’,’a’,’v’,’a’,’i’,’n’,’t’,’e’,’r’,’v’,’i’,’e’,’w’};
String copiedString="";
copiedString=copiedString.valueOf(str,0,4);
System.out.println("copying first four characters from str");
System.out.println(copiedString);
}

}

[/code]

Output:

copying first four characters from str
java

Oct 08

How to copy String using copyValueOf() in java

Copying String using copyValueOf() method in java

[code lang=”java”]package examples;
public class StringCopyValueOfExample {
public static void main(String[] args) {
char [] OrginalString={‘j’,’a’,’v’,’a’};
String copiedString="";
copiedString=copiedString.copyValueOf(OrginalString);
System.out.println("Copied String"+copiedString);

}

}

[/code]

Output;

Copied String java