0% found this document useful (0 votes)
8 views3 pages

Strings

The document contains Java code snippets demonstrating various programming concepts such as string manipulation, comparison, and formatting. It includes examples of using String methods, comparing String objects, and using StringBuilder for character sequences. Additionally, it lists common format specifiers for output formatting in Java.

Uploaded by

abhinavsrivas05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Strings

The document contains Java code snippets demonstrating various programming concepts such as string manipulation, comparison, and formatting. It includes examples of using String methods, comparing String objects, and using StringBuilder for character sequences. Additionally, it lists common format specifiers for output formatting in Java.

Uploaded by

abhinavsrivas05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.

kunal;
System.out.println(name.indexOf('a'));
public class Comparison {
System.out.println(" Kunal
public static void main(String[] args) {
".strip());
String a = "Kunal";

String b = "Kunal"; System.out.println(Arrays.toString(name.s


plit(" ")));
String c = a;
}
// System.out.println(c == a);
}
// ==

// System.out.println(a == b);

String name1 = new String("Kunal");

String name2 = new String("Kunal"); package com.kunal;

System.out.println(name1 == name2);
import java.util.ArrayList;
System.out.println(name1.equals(name2))
;
public class Operators {
}
public static void main(String[] args) {
}
System.out.println('a' + 'b');

System.out.println("a" + "b");
package com.kunal;
System.out.println((char)('a' + 3));
import java.util.Arrays;

public class Methods {


System.out.println("a" + 1) // this is
public static void main(String[] args) { same as after a few steps: "a" + "1"

String name = "Kunal Kushwaha Hello // integer will be converted to Integer


World"; that will call toString()
System.out.println(Arrays.toString(name.t
System.out.println("Kunal" + new
oCharArray()));
ArrayList<>());

System.out.println("Kunal" + new
System.out.println(name.toLowerCase());
Integer(56));
System.out.println(name);
String ans = new Integer(56) + "" +
new ArrayList<>();
System.out.println(ans); public class Performance {

System.out.println("a" + 'b'); public static void main(String[] args) {

} String series = "";

} for (int i = 0; i < 26; i++) {

package com.kunal; char ch = (char)('a' + i);

public class Palin { series = series + ch; // series += ch

public static void main(String[] args) { }

String str = "abcba";

System.out.println(isPalindrome(str)); System.out.println(series);

} }

static boolean isPalindrome(String str) { }

if (str == null || str.length() == 0) {

return true;

} package com.kunal;

str = str.toLowerCase(); public class PrettyPrinting {

for (int i = 0; i <= str.length() / 2; i++) { public static void main(String[] args) {

char start = str.charAt(i); float a = 453.1274f;

char end = str.charAt(str.length() - 1 // System.out.printf("Formatted


- i); number is %.2f", a);

if (start != end) { // System.out.printf("Pie: %.3f",


Math.PI);
return false;

}
System.out.printf("Hello my name is
}
%s and I am %s", "Kunal", "Cool");
return true;
}
}
}
}
public class SB {
package com.kunal;
public static void main(String[] args) {
StringBuilder builder = new
StringBuilder();

for (int i = 0; i < 26; i++) {

char ch = (char)('a' + i);

builder.append(ch);

System.out.println(builder.toString());

builder.reverse();

System.out.println(builder);

There are many format specifiers we can


use. Here are some common ones:

%c – Character

%d - Decimal number (base 10)

%e - Exponential floating-point number

%f - Floating-point number

%i - Integer (base 10)

%o - Octal number (base 8)

%s - String

%u - Unsigned decimal (integer) number

%x - Hexadecimal number (base 16)

%t - Date/time

%n – Newline

You might also like