This question already has answers here:
Trim a string based on the string length
(11 answers)
Closed 1 year ago.
Say I have an unknown string of 100+ characters and I want to remove the last characters before the 100th character and replace them with ..., deleting every character after the 100th. ie:
I would like to remove the last three characters before the 100th character and replace them with a period and deleting everything after.
would become:
I would like to remove the last three characters before the 100th character and replace them with....
What is the best way to tackle this problem?
Let String original = ... denote the first text. Then I suspect the following String is what you seek:
String result = String.format("%s%s", original.substring(0, Math.min(100, original.length())), original.length() > 100 ? "..." : "")
Related
This question already has answers here:
Is there an easy way to output two columns to the console in Java?
(3 answers)
How do I properly align using String.format in Java?
(5 answers)
Closed last month.
I had a problem come up in class where we had to make sure there was a consistent gap between strings on the output. For example, say there were 2 pairs of strings: "112.3", "32.3"; and "232.3","167.8".
The goal would be to output the strings with enough space so there is at least 4 spaces between the tens place of the second number of the pair, and last character of the first pair as such for all of the given pairs (basically so that the last characters of the 2nd strings line up vertically):
122.3 32.3
232.3 167.8
Continued Example:
122.3 32.3
232.3 167.8
123.4 1567.3
123.4 1.2
I wondered if there was some regex or escape sequence that allowed this to be done easily but I didn't know of any so I resorted to using the following method (str1 is first string in pair and str2 is second).
My Solution:
`System.out.print(str1);
if(str.length - 4 >= 0){
for(int i = 0; i < 8-str2.length; i++){
System.out.print(" ");
}
}
System.out.print(str2);`
Is there a regex, escape sequence, or delimiter to easily allow for this or is this the best method for this?
This question already has answers here:
Substring in Java - length up to a value
(6 answers)
Closed 2 years ago.
System.out.print(str.substring(0,5));
In this case, it will abbreviate the input to the 5th character. But when I input strings that are less than 5 characters, an error is produced. I want the code to abbreviate to the 5th character but keep anything less than 5 characters in its original format.
So if the input was "Cat", I would want the output to be "Cat". But if the input was "Mathematics", I want the output to be "Mathe". So anything less than 5 characters is outputted normally, but anything above 5 is abbreviated to the 5th character.
You need to take a substring with at most 5 chars of length, this will do the trick:
str.substring(0, Math.min(str.length(), 5));
Try something like
str.substring(0,Math.min(str.length(), 5))
This question already has answers here:
How to remove duplicate white spaces in string using Java?
(9 answers)
Closed 6 years ago.
A String can contain multiple spaces in a row - I need to replace multiple subsequent spaces by one single space char. The "problem" is that i cant know how many spaces there may encounter. The function I look for shall not only replace the first occurance of a found match, but all multiple equal characters in a String.
I searched a lot on the internet and tried the regex "X*? (X, zero or more times)" which I found unter "Reluctant quantifiers" on https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum
That didnt work: s1 = s1.replaceAll(" *?", " ");
Where s1 = "Hello World"; should be converted to s1 = "Hello World";
I'd be thankful for any help.
You can use replaceAll() that replaces whitespaces with just a single space.
String st = "helllo world"
System.out.println(st.replaceAll("\\s+"," "))
Output : helllo world
This question already has answers here:
Removing duplicates from a String in Java
(50 answers)
Closed 7 years ago.
If I have a string "SSSAAADDDCCC" how would I print just "SADC". Can it be done using SubString or would I have to use charAt()?
There is a simple way to do this - however since I don't see any code and I do not see any effort on your part I will not just give you the answer. Below is some psudo code you can work off to try to find the right answer. Good Luck!
currentChar = myString.charAt(0);
i = 0;
print current character //as per comments, cover the base case
while(string has more characters)
if current character != next character
print next character
i++
Use regular expression to replace all repeating characters with a single character:
"SSSAAADDDCCC".replaceAll("(.)\\1+", "$1") // returns "SADC"
(.) matches and captures a character.
\\1+ matches one or more instances of the captured character.
$1 replaces the entire matched value with the captured character.
Non-repeating characters are not matched, and are therefore left alone.
If you don't like the charAt method you could use substrings like this:
int j=0;
String in="sssdddaaaccc";
String out="";
for(int i=0;i<4;i++)
{
out=out+in.subString(j,j+1);
for(j=j; j<3;j++);
}
System.out.println(out);
This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 8 years ago.
so I have been looking on here and I can find alot of solutions that either completely remove all white space or just remove spaces, or just remove tabs. Basically what I need/ want is a way to take a string, and turn all double spaces+ or tabs and turn them into a single space. ie
String temp = "This is a test for strings";
String result = "This is a test for strings";
any ideas? possible java library methods?
Use String.replaceAll:
String result = temp.replaceAll("\\s+", " ");
where \\s+ stands for more than one whitespace character.
You can use regExp with method #replaceAll other than that you can first use trim to remove leading and trailing spaces.
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " ");
Here \\s+ is regExp which means one or more spaces which will be replaced with single space by replaceAll method.
Try this:
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " "));