I wonder why the double quotation marks is not shown in the actual output - just after the equal sign:
String word = "" + c1 + c2 + "ll";
The full code as follows:
public class InstantMethodsIndexOf
{
public void start()
{
String greeting = new String ("Hello World");
int position = greeting.indexOf('r');
char c1 = greeting.charAt(position + 2);
char c2 = greeting.charAt(position - 1);
**String word = "" + c1 + c2 + "ll";**
System.out.println(word);
}
}
When you pass "" to a String you are passing an empty String. You need to escape the quotation with a back slash if you want to print them.
Example:
String word = "\"" + c1 + c2 + "ll\"";
then System.out.println(word) will print:
"Hell"
As you can see I am escaping one double quotation at the beginning and another at the end
(Assuming c1 == 'H' and c2 == 'e')
The quotation mark does not appear because you have none being printed. What you have is an empty string being concatenated with other contents.
If you need the quotation mark, then you shoud do the following:
String word = "\"" + c1 + c2 + "ll";
It's a way to let Java know that it will be a string straight from the beginning, since "" is a String object of an empty string.
In your code, it doesn't really look useful. But following is an example where it would be:
int a=10, b=20;
String word = a + b + "he"; // word = "30he"
String word2 = "" + a + b + "he"; // word2 = "1020he"
I wonder why the double quotation marks is not shown in the actual
output - just after the equal sign:
String word = "" + c1 + c2 + "ll";
You are declaring a String that concatenates:
The empty String ""
c1
c2
The String literal "ll"
To show the quotes and make the code easier to read, try:
String word = '\u0022' + c1 + c2 + "ll"
which uses the unicode character value to print the double quote
I wonder why the double quotation marks is not shown in the actual
output - just after the equal sign:
In java String represented by the use of double quotes means the data between double quotes is considered as String value but if you want to include double quotes you have to use escape character \".
Moreover I suggest you to use StringBuilder and append your characters and String into it and use toString to print.
String str="ABC";//So value of String literal is ABC not "ABC"
String empty="";//is just empty but NOT Null
String quote="\"";//Here String has value " (One Double Quote)
This code
String greeting = "Hello World"; // <-- no need for new String()
int position = greeting.indexOf('r'); // <-- 8
char c1 = greeting.charAt(position + 2); // <-- 'd'
char c2 = greeting.charAt(position - 1); // <-- 'o'
String word = "" + c1 + c2 + "ll"; // <-- "" + 'd' + 'o' + "ll"
The empty String "" is used to coerce the arithmetic to a String, so it could also be written as
StringBuilder sb = new StringBuilder();
sb.append(c1).append(c2).append("ll");
String word = sb.toString();
or
StringBuilder sb = new StringBuilder("ll");
sb.insert(0, c2);
sb.insert(0, c1);
String word = sb.toString();
If you wanted to include double quotes in your word, your could escape them with a \\ or use a character -
char c1 = greeting.charAt(position + 2); // <-- 'd'
char c2 = greeting.charAt(position - 1); // <-- 'o'
String word = "\"" + c1 + c2 + "ll\""; // <-- "\"" + 'd' + 'o' + "ll\""
or
String word = "" + '"' + c1 + c2 + "ll" + '"';
Related
My String:
BByTTheWay .I want to split the string as B By T The Way BByTheWay .That means I want to split string if I get any capital letters and last put the main string as it is. As far I tried in java:
public String breakWord(String fileAsString) throws FileNotFoundException, IOException {
String allWord = "";
String allmethod = "";
String[] splitString = fileAsString.split(" ");
for (int i = 0; i < splitString.length; i++) {
String k = splitString[i].replaceAll("([A-Z])(?![A-Z])", " $1").trim();
allWord = k.concat(" " + splitString[i]);
allWord = Arrays.stream(allWord.split("\\s+")).distinct().collect(Collectors.joining(" "));
allmethod = allmethod + " " + allWord;
// System.out.print(allmethod);
}
return allmethod;
}
It givs me the output: B ByT The Way BByTTheWay . I think stackoverflow community help me to solve this.
You may use this code:
Code 1
String s = "BByTTheWay";
Pattern p = Pattern.compile("\\p{Lu}\\p{Ll}*");
String out = p.matcher(s)
.results()
.map(MatchResult::group)
.collect(Collectors.joining(" "))
+ " " + s;
//=> "B By T The Way BByTTheWay"
RegEx \\p{Lu}\\p{Ll}* matches any unicode upper case letter followed by 0 or more lowercase letters.
CODE DEMO
Or use String.split using same regex and join it back later:
Code 2
String out = Arrays.stream(s.split("(?=\\p{Lu})"))
.collect(Collectors.joining(" ")) + " " + s;
//=> "B By T The Way BByTTheWay"
Use
String s = "BByTTheWay";
Pattern p = Pattern.compile("[A-Z][a-z]*");
Matcher m = p.matcher(s);
String r = "";
while (m.find()) {
r = r + m.group(0) + " ";
}
System.out.println(r + s);
See Java proof.
Results: B By T The Way BByTTheWay
EXPLANATION
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount possible))
As per requirements, you can write in this way checking if a character is an alphabet or not:
char[] chars = fileAsString.toCharArray();
StringBuilder fragment = new StringBuilder();
for (char ch : chars) {
if (Character.isLetter(ch) && Character.isUpperCase(ch)) { // it works as internationalized check
fragment.append(" ");
}
fragment.append(ch);
}
String.join(" ", fragment).concat(" " + fileAsString).trim(); // B By T The Way BByTTheWay
This question already has answers here:
Creating Unicode character from its number
(13 answers)
Closed 1 year ago.
How to put Unicode char U+1F604 in Java String?
I attempted using
String s = "\u1F604";
but it equivalent to
String s = "\u1F60"+"4";
it was split into 2 chars.
DuncG's answer is a good way of doing it. The short explanation for this is that Unicode characters, by default, only take up 4 bytes, so the string literal escape only allows \u####. However, emojis are surrogate pairs and Unicode has reserved U+D800 to U+DFFF for these pairs, allowing 1024 x 1024 pair characters.
A different way of doing it that doesn't require converting into UTF-16 and encoding as a surrogate pair is to use Character.toChars(...):
public class Main {
public static void main(String[] args) {
String s = "Hello " + new String(Character.toChars(0x1f604)) + "!";
System.out.println(s);
}
}
Try it online!
The third variant, especially Character.toString(0x1f604):
public class Main {
public static void main(String[] args) {
String s1 = "Hello " + Character.toString(0x1f604) + "!"; // Since Java 11
String s2 = "Hello " + new String(new int[]{0x1f604}, 0, 1) + "!"; // < 11
System.out.println(s1 + " " + s2);
}
}
(Notice that in some other languages \U0001f604 might be used. In java \u and \U are the same.)
The UTF-16 encoding of your character U+1F604 is 0xD83D 0xDE04, so it should be:
String s = "\uD83D\uDE04";
You can add this UTF-16 smiley face symbol to the string as a symbol itself, as a hexadecimal or decimal surrogate pair, or its supplementary code point.
// symbol itself
String str1 = "😄";
// surrogate pair
String str2 = "\uD83D\uDE04";
// surrogate pair to its supplementary code point value
int cp = Character.toCodePoint('\uD83D', (char) 0xDE04);
// since 11 - decimal codepoint to string
String str3 = Character.toString(cp);
// since 11 - hexadecimal codepoint to string
String str4 = Character.toString(0x1f604);
// output
System.out.println(str1 + " " + str2 + " " + str3 + " " + str4);
Output:
😄 😄 😄 😄
If you have a string representation of a hexadecimal value of a character, you can read a numeric value using Integer.parseInt method.
// surrogate pair
char high = (char) Integer.parseInt("D83D", 16);
char low = (char) Integer.parseInt("DE04", 16);
String str1 = new String(new char[]{high, low});
// supplementary code point
int cp = Integer.parseInt("1F604", 16);
char[] chars = Character.toChars(cp);
String str2 = new String(chars);
// since 11
String str3 = Character.toString(cp);
// output
System.out.println(str1 + " " + str2 + " " + str3);
Output:
😄 😄 😄
There are several ways to perform a String conversion in Java and some people (including myself) prefers to concatenate an empty string to do the conversion:
Example:
char ch = 'A';
String str = "" + ch; //gets character value and append to str
However the order of the empty String is always a mystery to me. The following will successfully perform a String conversion:
str = ch + "";
str = ch + "" + ch;
but not the following:
str = ch + ch + ""; //if (ch + "") gives us "A", shouldn't this be "65A"?
Que: To be safe, we can always place the empty String infront, but I want to know how Java interprets the concatenation when the (empty) string is placed in other locations (such as in between or at the back).
The + operator is left-associative, which means that it is grouped from left-to-right.
str = ch + ch + "";
This is equivalent to
str = (ch + ch) + "";
// = ('A' + 'A') + "";
// = 130 + "";
// = "130";
not
str = ch + (ch + "");
// = 'A' + ('A' + "");
// = 'A' + "A";
// = "AA";
char + String and String + char both result in a String. But char + char returns an int. Do you see now why a second + ch doesn't work?
From the below question I din't understand how the output has come. Could someone please explain me how did it come ?
public class mystery{
public static void main(String[] args) {
System.out.println(serios("DELIVER"));
}
public static String serios(String s)
{
String s1 = s.substring(0,1);
System.out.println(s1);
String s2 = s.substring(1, s.length() - 1);
System.out.println(s2);
String s3 = s.substring(s.length() - 1);
System.out.println(s3);
if (s.length() <= 3)
return s3 + s2 + s1;
else
return s1 + serios(s2) + s3;
}
}
Output:
D
ELIVE
R
E
LIV
E
L
I
V
DEVILER
Thanks !!
For this chunk of code
String s1 = s.substring(0,1);//this initializes s1 = D as Substring reads up to right before the ending index which is 1.
System.out.println(s1);//print s1
This chunk
String s2 = s.substring(1, s.length() - 1);//Starts where the previous chunk left off, ends right before the ending initializing s2 = ELIVE
System.out.println(s2);//print s2
Final Chunk
String s3 = s.substring(s.length() - 1);//This chunk starts from the end and captures R
System.out.println(s3);//print s3
These three chunks and their print statements will give you
D ELIVE R
Now let's move on.
The final return statement returns s1 + serios(s2) + s3. This is recursion, a function called within itself.
This recursion will run until the if condition is met. Finally printing out DELIVER
You can see a pattern to understand better.
DELIVER when run through the code is printed out like this D ELIVE R. The first and last letters are separated from the center of the word.
return s1 + serios(s2) + s3;
since s2 = ELIVE it will become equal to s. It will be split apart using substring just like DELIVER to become E LIV E setting
LIV = s2
s will now equal LIV, and be split apart and printed out as
L I V
Finally the length of s is equal to 3, so the if condition will run and print out DEVILER
apart of what subString is doing, I think your problem is about recursive behavior of the series method.
at first call u send "DELIVER".
at the following line you can see if the input param is grater than 3 the method call itself agin this time with s2. for the first iteration s2 = ELIVE.
if (s.length() <= 3)
return s3 + s2 + s1;
else
return s1 + serios(s2) + s3;
you can think about running series("ELIVE"); and for the same process you will see this time s2 will get "LIV" which the recursion do not happen again and if part will run.
if (s.length() <= 3)
return s3 + s2 + s1;
I hope this help you.
For this type of tasks, it helps to trace the method calls
public class mystery {
public static void main(String[] args)
{
serios("DELIVER", "");
}
public static String serios(String s, String indentation)
{
String s1 = s.substring(0, 1);
System.out.println(indentation + "\"" + s1 + "\" is the substring of \"" + s + "\" at 0");
String s2 = s.substring(1, s.length() - 1);
System.out.println(indentation + "\"" +s2 + "\" is the substring of \"" + s + "\" from 1 to " + (s.length() - 2));
String s3 = s.substring(s.length() - 1);
System.out.println(indentation + "\"" + s3 + "\" is the substring of \"" + s + "\" at " + (s.length() - 1));
if (s.length() <= 3)
return s3 + s2 + s1;
else
{
indentation += " ";
return s1 + serios(s2, indentation) + s3;
}
}
}
Output:
"D" is the substring of "DELIVER" at 0
"ELIVE" is the substring of "DELIVER" from 1 to 5
"R" is the substring of "DELIVER" at 6
"E" is the substring of "ELIVE" at 0
"LIV" is the substring of "ELIVE" from 1 to 3
"E" is the substring of "ELIVE" at 4
"L" is the substring of "LIV" at 0
"I" is the substring of "LIV" from 1 to 1
"V" is the substring of "LIV" at 2
I have a String
String str = (a AND b) OR (c AND d)
I tokenise with the help of code below
String delims = "AND|OR|NOT|[!&|()]+"; // Regular expression syntax
String newstr = str.replaceAll(delims, " ");
String[] tokens = newstr.trim().split("[ ]+");
and get String[] below
[a, b, c, d]
To each element of the array I add " =1" so it becomes
[a=1, b=1, c=1, d=1]
NOW I need to replace these values to the initial string making it
(a=1 AND b=1) OR (c=1 AND d=1)
Can someone help or guide me ? The initial String str is arbitrary!
This answer is based on #Michael's idea (BIG +1 for him) of searching words containing only lowercase characters and adding =1 to them :)
String addstr = "=1";
String str = "(a AND b) OR (c AND d) ";
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("[a-z]+");
Matcher m = pattern.matcher(str);
while (m.find()) {
m.appendReplacement(sb, m.group() + addstr);
}
m.appendTail(sb);
System.out.println(sb);
output
(a=1 AND b=1) OR (c=1 AND d=1)
Given:
String str = (a AND b) OR (c AND d);
String[] tokened = [a, b, c, d]
String[] edited = [a=1, b=1, c=1, d=1]
Simply:
for (int i=0; i<tokened.length; i++)
str.replaceAll(tokened[i], edited[i]);
Edit:
String addstr = "=1";
String str = "(a AND b) OR (c AND d) ";
String delims = "AND|OR|NOT|[!&|() ]+"; // Regular expression syntax
String[] tokens = str.trim().split( delims );
String[] delimiters = str.trim().split( "[a-z]+"); //remove all lower case (these are the characters you wish to edit)
String newstr = "";
for (int i = 0; i < delimiters.length-1; i++)
newstr += delimiters[i] + tokens[i] + addstr;
newstr += delimiters[delimiters.length-1];
OK now the explanation:
tokens = [a, b, c, d]
delimiters = [ "(" , " AND " , ") OR (" , " AND " , ") " ]
When iterating through delimiters, we take "(" + "a" + "=1".
From there we have "(a=1" += " AND " + "b" + "=1".
And on: "(a=1 AND b=1" += ") OR (" + "c" + "=1".
Again : "(a=1 AND b=1) OR (c=1" += " AND " + "d" + "=1"
Finally (outside the for loop): "(a=1 AND b=1) OR (c=1 AND d=1" += ")"
There we have: "(a=1 AND b=1) OR (c=1 AND d=1)"
How long is str allowed to be? If the answer is "relatively short", you could simply do a "replace all" for every element in the array. This obviously is not the most performance-friendly solution, so if performance is an issue, a different solution would be desireable.