if I have a long String of text, what command or method should i use in order to leave exactly two whitespaces in front of each line?
Suppose
String sentences = "The car is blue.
The sky is blue.
The house is blue."
The output should be
The car is blue.
The sky is blue.
The house is blue.
public static String indentByTwoSpaces(String text)
{
String[] words = text.split(" ");
}
Simple use String.replace
System.out.println (" " + str.replaceAll("(\r\n|\n)", "\r\n "));
1. Split the lines using regex.
2. Use the loop for iteration for printing the contents by formatting the
string provided
NOTE- in case you need to concatenate the string with two spaces in the start then just split and concatenate the string with two spaces <" "+ --- > and return
String splittedLines[] = text.split("\\r?\\n");
int index = 0;
while(index != splittedLines.length) {
String.format(" %s", splittedLines[index]);
index++;
}
It entirely depends on your input.
see this below, this will print out your desired output, given the input does not have spaces after the period. you can adjust accordingly if there is.
public class TestMain {
public static void main(String[] args) {
// notice how my text input doesnt have spaces after the periods
String text = "The car is blue.The sky is blue.The house is blue.";
StringBuilder sb = new StringBuilder();
String splittedLines[] = text.split("\\.");
int index = 0;
while(index != splittedLines.length) {
// if there are spaces after the periods, consider adjusting this
sb.append(" ");
sb.append(splittedLines[index]);
sb.append("\n");
index++;
}
System.out.print(sb);
}
}
As per your requirement, you would need to split lines accordingly identifying sentences. However as per the given sample set of lines i would notice the full-stop is there after end of each sentences. Therefore i would suggest to use (.) with regular expression to split lines accordingly. Then you can format your lines accordingly as i shown below.
public class AddingSpaces {
public static void main(String[] args) {
// As shown below, you can have your own sentence even with or without spaces
String text = "The car is blue.The sky is blue.The house is blue.";
// Since there are multiple sentences, you can split it from full-stop
String splittedLines[] = text.split("\\.");
for (String line: splittedLines) {
StringBuilder sb = new StringBuilder();
sb.append(" ").append(line);
//Print the lines accordingly
System.out.println(sb);
}
}
}
Related
I am trying to replace all the . in a string except numbers like 1.02
I have a string : -
String rM = "51.3L of water is provided. 23.3L is used."
If I use rM.replaceAll() then every dot will be replaced, I want my string to be : -
51.3L of water is provided 23.3L is used
Is it possible to do in java?
I am not a java developer but can you try it with a pattern like below.
rM = rM.replaceAll("(?<=[a-z\\s])\\.", "");
replaceAll() with the right regex can do it for you.
This uses a negative look-ahead and look-behind to look for a '.' not in the middle of a decimal number.
rM.replaceAll("(?<![\\d])\\.(?![\\d]+)", "")
yes its possible. Something like the following should work. The regex should just check that the element starts with a character 0-9. If yes, don't change the element. If no, replace any . with the empty string.
String rM = "51.3L of water is provided. 23.3L is used.";
String[] tokens = rM.split(" ");
StringBuffer buffer = new StringBuffer();
for (String element : tokens) {
if (element.matches("[0-9]+.*")) {
buffer.append(element + " ");
} else {
buffer.append(element.replace(".", "") + " ");
}
}
System.out.println(buffer.toString());
Output:
51.3L of water is provided 23.3L is used
Here's a simple approach that assumes you want to get rid of dots that are placed directly after a char which isn't a whitespace.
The following code basically splits the sentence by whitespace(s) and removes trailing dots in every resulting character sequence and joins them afterwards to a single String again.
public static void main(String[] args) {
// example sentence
String rM = "51.3L of water is provided. 23.3L is used.";
// split the sentence by whitespace(s)
String[] parts = rM.split("\\s+");
// go through all the parts
for (int i = 0; i < parts.length; i++) {
// check if one of the parts ends with a dot
if (parts[i].endsWith(".")) {
// if it does, replace that part by itself minus the trailing dot
parts[i] = parts[i].substring(0, parts[i].length() - 1);
}
}
// join the parts to a sentence String again
String removedUndesiredDots = String.join(" ", parts);
// and print that
System.out.println(removedUndesiredDots);
}
The output is
51.3L of water is provided 23.3L is used
Using negative lookahead you can use \.(?![\d](\.[\d])?).
private static final String DOTS_NO_NUM_REGEX = "\\.(?![\\d](\\.[\\d])?)";
private static final Pattern PATTERN = Pattern.compile(DOTS_NO_NUM_REGEX);
public static void main(String[] args)
{
String s = "51.3L of water is provided. 23.3L is used.";
String replaced = PATTERN.matcher(s).replaceAll("");
System.out.println(replaced);
}
Output:
51.3L of water is provided 23.3L is used
I have a question about replacing words. I have some strings, each of which looks like this:
String string = "today is a (happy) day, I would like to (explore) more about Java."
I need to replace the words that have parentheses. I want to replace "(happy)" with "good", and "(explore)" with "learn".
I have some ideas, but I don't know how.
for (int i = 0; i <= string.length(), i++) {
for (int j = 0; j <= string.length(), j++
if ((string.charAt(i)== '(') && (string.charAt(j) == ')')) {
String w1 = line.substring(i+1,j);
string.replace(w1, w2)
}
}
}
My problem is that I can only replace one word with one new word...
I am thinking of using a scanner to prompt me to give a new word and then replace it, how can I do this?
The appendReplacement and appendTail methods of Matcher are designed for this purpose. You can use a regex to scan for your pattern--a pair of parentheses with a word in the middle--then do whatever you need to do to determine the string to replace it with. See the javadoc.
An example, based on the example in the javadoc. I'm assuming you have two methods, replacement(word) that tells what you want to replace the word with (so that replacement("happy") will equal "good" in your example), and hasReplacement(word) that tells whether the word has a replacement or not.
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher m = p.matcher(source);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String word = m.group(1);
String newWord = hasReplacement(word) ? replacement(word) : m.group(0);
m.appendReplacement(sb, newWord); // appends the replacement, plus any not-yet-used text that comes before the match
}
m.appendTail(sb); // appends any text left over after the last match
String result = sb.toString();
Use below code for replacing the string.
String string = "today is a (happy) day, I would like to (explore) more about Java.";
string = string.replaceAll("\\(happy\\)", "good");
string = string.replaceAll("\\(explore\\)", "learn");
System.out.println(string);`
What you can do is run a loop from 0 to length-1 and if loop encounters a ( then assign its index to a temp1 variable. Now go on further as long as you encounter ).Assign its index to temp2 .Now you can replace that substring using string.replace(string.substring(temp1+1,temp2),"Your desired string")).
No need to use the nested loops. Better use one loop and store the index when you find opening parenthesis and also for close parenthesis and replace it with the word. Continue the same loop and store next index. As you are replacing the words in same string it changes the length of string you need to maintain copy of string and perform loop and replace on different,
Do not use nested for loop. Search for occurrences of ( and ). Get the substring between these two characters and then replace it with the user entered value. Do it till there are not more ( and ) combinations left.
import java.util.Scanner;
public class ReplaceWords {
public static String replaceWords(String s){
while(s.contains(""+"(") && s.contains(""+")")){
Scanner keyboard = new Scanner(System.in);
String toBeReplaced = s.substring(s.indexOf("("), s.indexOf(")")+1);
System.out.println("Enter the word with which you want to replace "+toBeReplaced+" : ");
String replaceWith = keyboard.nextLine();
s = s.replace(toBeReplaced, replaceWith);
}
return s;
}
public static void main(String[] args) {
String myString ="today is a (happy) day, I would like to (explore) more about Java.";
myString = replaceWords(myString);
System.out.println(myString);
}
}
This snippet works for me, just load the HashMap up with replacements and iterate through:
import java.util.*;
public class Test
{
public static void main(String[] args) {
String string = "today is a (happy) day, I would like to (explore) more about Java.";
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("\\(happy\\)", "good");
hm.put("\\(explore\\)", "learn");
for (Map.Entry<String, String> entry : hm.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
string = string.replaceAll(key, value);
}
System.out.println(string);
}
}
Remember, replaceAll takes a regex, so you want it to display "\(word\)", which means the slashes themselves must be escaped.
There's a string
String str = "ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
How do I split it into strings like this
"ggg;ggg;"
"nnn;nnn;"
"aaa;aaa;"
"xxx;xxx;"
???????
Using Regex
String input = "ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
Pattern p = Pattern.compile("([a-z]{3});\\1;");
Matcher m = p.matcher(input);
while (m.find())
// m.group(0) is the result
System.out.println(m.group(0));
Will output
ggg;ggg;
nnn;nnn;
aaa;aaa;
xxx;xxx;
I assume that the you only want to check if the last segment is similar and not every segment that has been read.
If that is not the case then you would probably have to use an ArrayList instead of a Stack.
I also assumed that each segment has the format /([a-z])\1\1/.
If that is not the case either then you should change the if statement with:
(stack.peek().substring(0,index).equals(temp))
public static Stack<String> splitString(String text, char split) {
Stack<String> stack = new Stack<String>();
int index = text.indexOf(split);
while (index != -1) {
String temp = text.substring(0, index);
if (!stack.isEmpty()) {
if (stack.peek().charAt(0) == temp.charAt(0)) {
temp = stack.pop() + split + temp;
}
}
stack.push(temp);
text = text.substring(index + 1);
index = text.indexOf(split);
}
return stack;
}
Split and join them.
public static void main(String[] args) throws Exception {
String data = "ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
String del = ";";
int splitSize = 2;
StringBuilder sb = new StringBuilder();
for (Iterable<String> iterable : Iterables.partition(Splitter.on(del).split(data), splitSize)) {
sb.append("\"").append(Joiner.on(del).join(iterable)).append(";\"");
}
sb.delete(sb.length()-3, sb.length());
System.out.println(sb.toString());
}
Ref : Split a String at every 3rd comma in Java
Use split with a regex:
String data="ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
String [] array=data.split("(?<=\\G\\S\\S\\S;\\S\\S\\S);");
S: A non-whitespace character
G: last match/start of string, think of it of a way to skip delimiting if the
previous string matches current one.
?<=:positive look-behind will match semicolon which has string behind it.
Some other answer, that only works given your specific example input.
You see, in your example, there are two similarities:
All patterns seem to have exactly three characters
All patterns occur exactly twice
In other words: if those two properties are really met for all your input, you could avoid splitting - as you know exactly what to find in each position of your string.
Of course, following the other answers for "real" splitting are more flexible; but (theoretically), you could just go forward and do a bunch of substring calls in order to directly access all elements.
I am a beginner in Java trying to write a program to convert strings into title case. For example, if String s = "my name is milind", then the output should be "My Name Is Milind".
import java.util.*;
class TitleCase
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("ent");
String s=in.nextLine();
String str ="";
char a ;
for(int i =0;i<s.length()-1;i++)
{
a = s.charAt(i);
if(a==' ')
{
str = str+(Character.toUpperCase(s.charAt(i+1)));
}
else
{
str =str+(Character.toLowerCase(a));
}
}
//for(int i =0; i<s.length();i++)
//{
System.out.println(str);
//}
}
}
You are trying to capitalize every word of the input.
So you have to do following steps:
get the words separated
capitalize each word
put it all together
print it out
Example Code:
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("ent");
String s=in.nextLine();
//now your input string is storred inside s.
//next we have to separate the words.
//here i am using the split method (split on each space);
String[] words = s.split(" ");
//next step is to do the capitalizing for each word
//so use a loop to itarate through the array
for(int i = 0; i< words.length; i++){
//we will save the capitalized word in the same place again
//first, geht the character on first position
//(words[i].charAt(0))
//next, convert it to upercase (Character.toUppercase())
//then add the rest of the word (words[i].substring(1))
//and store the output back in the array (words[i] = ...)
words[i] = Character.toUpperCase(words[i].charAt(0)) +
[i].substring(1);
}
//now we have to make a string out of the array, for that we have to
// seprate the words with a space again
//you can do this in the same loop, when you are capitalizing the
// words!
String out = "";
for(int i = 0; i<words.length; i++){
//append each word to out
//and append a space after each word
out += words[i] + " ";
}
//print the result
System.out.println(out);
}
Using Java 8 streams:
String titleCase = (new ArrayList<>(Arrays.asList(inputString.toLowerCase().split(" "))))
.stream()
.map(word -> Character.toTitleCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.joining(" "));
The problem is with the way you're adding characters. Take a look at your if condition:
a = s.charAt(i);
if(a==' ')
{
// Here you are adding not the current character, but the NEXT character.
str = str+(Character.toUpperCase(s.charAt(i+1)));
}
else
{
// Here you are adding the current character.
str =str+(Character.toLowerCase(a));
}
As a result of this condition, you will skip a character if your input string contains a space, then repeat another character that you've already added.
Additionally, you're not looping through the whole string because your loop conditional goes to s.length()-1. Change that to just s.length(). However, if you do that, you may run into an exception if the input string ends with a space (since you'll try to check for a character at an out-of-bound index).
Here's what the fixed code would look like:
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("ent");
String s=in.nextLine();
String str ="";
char a ;
for(int i =0;i<s.length();i++)
{
a = s.charAt(i);
if(a==' ')
{
str = str+Character.toLowerCase(a)+(Character.toUpperCase(s.charAt(i+1)));
i++; // "skip" the next element since it is now already processed
}
else
{
str =str+(Character.toLowerCase(a));
}
}
System.out.println(str);
}
NOTE: I only fixed the code that you supplied. However, I'm not sure it works the way you want it to - the first character of the string will still be whatever case it started in. Your conditional only uppercases letters that are preceded by a space.
You want to change the case of the first letter of each word of a String.
To do so, I would follow the following steps :
split the String in words : see String.split(separator)
retrieve the first letter of each word : see String.charAt(index)
retrieve its capitalized version : the Character.toUpperCase(char) you use is perfect
concatenate the capitalized letter with the rest of the word : concatenation operator (+) and String.substring
create a new String from the capitalized words : see String.join(separator)
Code Golf variation... I challenge anyone to make it any simpler than this:
public String titleCase(String str) {
return Arrays
.stream(str.split(" "))
.map(String::toLowerCase)
.map(StringUtils::capitalize)
.collect(Collectors.joining(" "));
}
By the way: Unicode distinguishes between three cases: lower case, upper case and title case. Although it does not matter for English, there are other languages where the title case of a character does not match the upper case version. So you should use
Character.toTitleCase(ch)
instead of Character.toUpperCase(ch) for the first letter.
There are three character cases in Unicode: upper, lower, and title. Uppercase and lowercase are familiar to most people. Titlecase distinguishes characters that are made up of multiple components and are written differently when used in titles, where the first letter in a word is traditionally capitalized. For example, in the string "ljepotica",[2] the first letter is the lowercase letter lj(\u01C9 , a letter in the Extended Latin character set that is used in writing Croatian digraphs). If the word appeared in a book title, and you wanted the first letter of each word to be in uppercase, the correct process would be to use toTitleCase on the first letter of each word, giving you "Ljepotica" (using Lj, which is \u01C8). If you incorrectly used toUpperCase, you would get the erroneous string "LJepotica" (using LJ, which is \u01C7).
[The Java™ Programming Language, Fourth Edition, by James Gosling, Ken Arnold, David Holmes (Prentice Hall). Copyright 2006 Sun Microsystems, Inc., 9780321349804]
WordUtils.capitalizeFully() worked for me like charm as it gives: WordUtils.capitalizeFully("i am FINE") = "I Am Fine"
import java.util.Scanner;
public class TitleCase {
public static void main(String[] args) {
System.out.println("please enter the string");
Scanner sc1 = new Scanner(System.in);
String str = sc1.nextLine();
//whatever the format entered by user, converting it into lowercase
str = str.toLowerCase();
// converting string to char array for
//performing operation on individual elements
char ch[] = str.toCharArray();
System.out.println("===============");
System.out.println(str);
System.out.println("===============");
//First letter of senetence must be uppercase
System.out.print((char) (ch[0] - 32));
for (int i = 1; i < ch.length; i++) {
if (ch[i] == ' ') {
System.out.print(" " + (char) (ch[i + 1] - 32));
//considering next variable after space
i++;
continue;
}
System.out.print(ch[i]);
}
}
}
You can use lamda instead-
String titalName = Arrays.stream(names.split(" "))
.map(E -> String.valueOf(E.charAt(0))+E.substring(1))
.reduce(" ", String::concat);
I have been using apache WordUtils to capitalize every first letter in a String, but I need only the first letter of certain words to be capitalized and not all of them. Here is what I have:
import org.apache.commons.lang.WordUtils;
public class FirstCapitalLetter {
public static void main(String[] args) {
String str = "STATEMENT OF ACCOUNT FOR THE PERIOD OF";
str = WordUtils.capitalizeFully(str);
System.out.println(str);
}
}
My Output is:
Statement Of Account For The Period Of
I want my output to be
Statement of Account for the Period of
How can I achieve this?
1) Create a set of String you do not want to capitalize (a set of exceptions):
Set<String> doNotCapitalize = new HashSet<>();
doNotCapitalize.add("the");
doNotCapitalize.add("of");
doNotCapitalize.add("for");
...
2) Split the string by spaces
String[] words = "STATEMENT OF ACCOUNT FOR THE PERIOD OF".split(" ");
3) Iterate through the array, capitalizing only those words that are not in the set of exceptions:
StringBuilder builder = new StringBuilder();
for(String word : words){
String lower = word.toLowerCase();
if(doNotCapitalize.contains(lower){
builder.append(lower).append(" ");
}
else{
builder.append(WordUtils.capitalizeFully(lower)).append(" ");
}
}
String finalString = builder.toString();
Run WordUtils.capitalizeFully for each word in your string only if its length is longer than 3, this will work in this specific case
U need to break the string in three parts
1. Statement of
2. Account for the
3. Period of.