What is use of "%" and "s" in below code? [duplicate] - java

This question already has answers here:
How to format strings in Java
(10 answers)
How to use String.format() in Java? [duplicate]
(2 answers)
Closed 8 days ago.
What is use of "%" and "s" in below code?
public class Pattern {
public static void main(String[] args) {
staircase(6);
}
public static void staircase(int n) {
String hash = "";
for (int i = 0; i < n; i++){
hash += "#";
System.out.println(String.format("%" + n +"s" , hash));
// ^^^ ^^^
}
}
}

Related

My String.split() function is not running properly and I don't know why? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 2 years ago.
public class fake {
public static void main (String[] args) {
String s = "i.like.this.program.very.much";
String arr[] = s.split(".");
int n = arr.length;
System.out.println(n);
}
my output is coming '0'
You need to escape the dot (.) character, And why you need to do this is because(.) is the special character in Java Regular Expression
String s = "i.like.this.program.very.much";
String arr[] = s.split("\\.");
int n = arr.length;
System.out.println(n);
Another way could be :
String s = "i.like.this.program.very.much";
String arr[] = s.split("[.]");
int n = arr.length;
System.out.println(n);

Add a character after every 10 characters in a string using subString() function [duplicate]

This question already has answers here:
Putting char into a java string for each N characters
(12 answers)
Add line break after every 60 characters in String for Java [duplicate]
(1 answer)
Closed 6 years ago.
Suppose the string is like this:
String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
I want to add a character supposing 'f' after every 10 character iteration using subString function because I can't call StringBuilder class ( used it for insert or append functionality).
public class HelloWorld{
public static void main(String []args){
int i = 10;
String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
while(i <= msg.length())
{
msg = msg.substring(0, i) + "f" + msg.substring(i, msg.length());
i = i + 11;
}
System.out.println(msg);
}
}
It may help you
public static String insertPeriodically(String text, String insert, int period)
{
StringBuilder builder = new StringBuilder(text.length() + insert.length() * (text.length()/period)+1);
int index = 0;
String prefix = "";
while (index < text.length())
{
// Don't put the insert in the very first iteration.
// This is easier than appending it *after* each substring
builder.append(prefix);
prefix = insert;
builder.append(text.substring(index,
Math.min(index + period, text.length())));
index += period;
}
return builder.toString();
}

Break down a long string into smaller string of given length [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I am trying to break down a long given string into a smaller string of given length x, and it returns an array of these small strings. But I couldn't print out, it gives me error [Ljava.lang.String;#6d06d69c
Please take a look at my code and help me out if I am doing wrong. Thanks so much!
public static String[] splitByNumber(String str, int num) {
int inLength = str.length();
int arrayLength = inLength / num;
int left=inLength%num;
if(left>0){++arrayLength;}
String ar[] = new String[arrayLength];
String tempText=str;
for (int x = 0; x < arrayLength; ++x) {
if(tempText.length()>num){
ar[x]=tempText.substring(0, num);
tempText=tempText.substring(num);
}else{
ar[x]=tempText;
}
}
return ar;
}
public static void main(String[] args) {
String[] str = splitByNumber("This is a test", 4);
System.out.println(str);
}
You're printing the array itself. You want to print the elements.
String[] str = splitByNumber("This is a test", 4);
for (String s : str) {
System.out.println(s);
}

How to write encoded string into database after doing some processing java [duplicate]

This question already has answers here:
How do I escape a string in Java?
(5 answers)
Closed 7 years ago.
I want to print \u0070\u0075\u0062 as output of this program, how can I do this in Java.
public class ankit {
public static void main(String[]args){
String s="\u0070\u0075\u0062";
System.out.println(s);
}
}
it gives me "pub" as output
actually i am sending it to a database and if there is unicode for new line charcter "\n" my bulk import to mySQL is not proper. so I want to store the string as unicode encoded when writing to mysql
i dont want to convert unicode for "\n" to actual newline while writing into db
you can do like that:
String s="\\u0070\\u0075\\u0062";
than you can get the ans
This should help you do the trick :)
public class Test {
public static void main(String[] args) {
String s = "\u0070\u0075\u0062";
StringBuffer sbuffer = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch >= '\u0000' && ch <= '\u01FF') {
String ss = Integer.toHexString(ch);
sbuffer.append("\\\\u");
for (int k = 0; k < 4 - ss.length(); k++) {
sbuffer.append('0');
}
sbuffer.append(ss.toUpperCase());
} else {
sbuffer.append(ch);
}
}
System.out.println(sbuffer);
}
}
source : http://arpan-khandelwal-tech.blogspot.in/2008/08/this-article-is-related-to-escaping-of.html

Checking if a word is a Palindrome or not [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
So when I'm running this code with any word, it is always returning false. The first String accepts the word, and then it's changed to lower case. Then I'm building a new String out of it to compare it to another string that is appended as the reverse of the original word. Am I not seeing something, or can you tell me what's wrong with it?
public class Palindromes
{
public static void main(String[] args)
{
int count = Integer.parseInt(args[0]);
for(int i = 1; i <= count; i++)
{
System.out.print(isPalindrome(args[i]) + " ");
}
}
public static boolean isPalindrome(String s)
{
String str = s.toLowerCase();
StringBuilder orig_str = new StringBuilder(str);
StringBuilder revStr = new StringBuilder();
for (int i = str.length()-1; i >= 0; i--)
{
revStr.append(orig_str.charAt(i));
}
boolean isPal = (revStr == orig_str);
return isPal;
}
}
Comparing two distinct StringBuilder instances with == would always give you false, regardless of their content, since they are not the same instance.
Try revStr.toString().equals(str)
It seems that StringBuilder doesn't override Object's equals, so you have to perform the equals on the Strings that result from the StringBuilders.
BTW, StringBuilder has a reverse method, so you can re-write your method in a single line :
public static boolean isPalindrome(String s) {
return new StringBuilder(s.toLowerCase()).reverse().toString().equals(s.toLowerCase());
}

Categories