Monday, June 1, 2015

Java Strings

matches() :

package stringMethods2;

public class Matches {

public static void main(String[] args) {

String str = "Welcome to my hood";

System.out.println(str.matches("(.*)my(.*)"));
System.out.println(str.matches("(.*)my"));
System.out.println(str.matches("my(.*)"));
System.out.println(str.matches("Welcome(.*)"));

}

}



regionMatches() :

package stringMethods2;

// public boolean regionMatches(int toffset, String other, int ooffset, int len)
// or
// regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)

public class regionMatches {

public static void main(String[] args) {

String str = "Welcome to my hood";
String str2 = "to";
String str3 = "TO";

System.out.println(str.regionMatches(8 , str2 , 0, 2));
System.out.println(str.regionMatches(8 , str3 , 0, 2));
System.out.println(str.regionMatches(true, 8, str3, 0, 2));

}
}



replace() :


package stringMethods2;

// public String replace(char oldChar,char newChar)

public class replace {

public static void main(String[] args) {

String str1 = "Welcome to my tutorial";

System.out.println(str1.replace('m', 'X'));

}
}


replaceAll() :

package stringMethods2;

// public String replaceAll(String regex,String replacement)

public class replaceAll {

public static void main(String[] args) {

String str1 = "welcome to my hood";

System.out.println(str1.replaceAll("(.*)to(.*)", "ReplacedString"));
System.out.println(str1.replaceAll("to(.*)", "ReplacedString"));
System.out.println(str1.replaceAll("(.*)to", "ReplacedString"));

}
}



replaceFirst() :


package stringMethods2;

public class ReplaceFirst {

public static void main(String[] args) {

String name = "Welcome to my hood";

System.out.println(name.replaceFirst("hood", "replacementString"));

}
}


split() :

package stringMethods2;

// public String[] split(String regex,int limit)
// or
// public String[] split(String regex)

public class split {

public static void main(String[] args) {

String str1 =new String( "Welcome to-my hood");

//System.out.println(str1.split("-"));
//System.out.println(str1.split("-",5)); It gives the reference code

for (String string : str1.split("-")) {
System.out.println(string);
}

System.out.println("\n----------------\n");

for(String string2 : str1.split("o", 4)){
System.out.println(string2);
}
}
}



startsWith() :


package stringMethods2;

// public boolean startsWith(String prefix,int toffset)
// or
// public boolean startsWith(String prefix)

public class startsWith {

public static void main(String[] args) {

String str = "Welcome to my hood";

System.out.println(str.startsWith("Welcome"));
System.out.println(str.startsWith("welcome"));
System.out.println(str.startsWith("Welcome",0));
System.out.println(str.startsWith("Welcome",1));

}
}


subSequence() :

package stringMethods2;

// public CharSequence subSequence(int beginIndex,int endIndex)

public class subSequence {

public static void main(String[] args) {

String name = "honey bunny";
System.out.println(name.subSequence(2, 10));
}
}



subString() :

package stringMethods2;

// public String substring(int beginIndex)
// or
// public String substring(int beginIndex,int endIndex)

public class subString {

public static void main(String[] args) {

String str = "Welcome to my hood" ;

System.out.println(str.substring(3));
System.out.println(str.substring(1, 11));
}
}


toCharArray() :


package stringMethods2;

// public char[] ToCharArray()

public class ToCharArray {

public static void main(String[] args) {

String str = new String("Welcome honey.com");
System.out.println(str.toCharArray());
}
}


toUppercase() / toLowercase() :


package stringMethods2;

// public String toLowerCase()
// or
// public String toLowerCase(Locale locale)

// public String toUpperCase()
// or
// public String toUpperCase(Locale locale)

public class toLowerAndUpperCase {

public static void main(String[] args) {

String name = "Bipen";
System.out.println(name.toLowerCase());
System.out.println(name.toUpperCase());
}
}


trim() :


package stringMethods2;

// publicString trim(
// It returns a copy of this string with leading and trailing white space removed, or this string if it
// has no leading or trailing white space.

public class trim {

public static void main(String[] args) {

String str = "              Welcome to my hood.       ";
System.out.println(str.trim());
}
}


valueOf() :


package stringMethods2;

// This method returns the string representation of the passed argument.
// static String valueOf(boolean b)
// static String valueOf(char c)
// static String valueOf(char[] data)
// static String valueOf(char[] data,int offset,int count)
// static String valueOf(double d)
// static String valueOf(float f)
// static String valueOf(int i)
// static String valueOf(long l)
// static String valueOf(Object obj)

public class ValueOf {

public static void main(String[] args) {

int a = 1;
double b = 2.0 ;
float c = 3f ;
boolean d = true;
char[] ch = {'a','s','d','f','g','h','j','k','l'};

System.out.println(String.valueOf(a));
System.out.println(String.valueOf(b));
System.out.println(String.valueOf(c));
System.out.println(String.valueOf(d));
System.out.println(String.valueOf(ch));
}

}


No comments:

Post a Comment