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))
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:
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 ? "..." : "")
This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 2 years ago.
I have the following strings where I would like to identify numbers, that can be sometimes followed by any character. For example:
Cats 10, Dogs 3?
Cats 10 Dogs 3
Cats 10. Dogs 3#
I should be able to find 10 and 3 in each of these strings.
I tried
s.matches("[0-9,.#!]+")) but in this case I don't catch numbers not followed by special characters.
Can you use the below?
s.matches("\d+")
maybe try
s.matches("[0-9].?");
. is any character
? is zero or more
This question already has answers here:
Ignoring white space for a Regex match
(3 answers)
How to ignore white space with regex?
(2 answers)
Closed 2 years ago.
I am trying to match
123 6523 8745
1234 65 3212
etc many combinations
The i want the regex to ignore the spaces and count 10 digits in a single line. For example [\d]{10} doesn't ignore the spaces, it will match any contiguous sequence of 10 digits. But here I want to ignore the spaces and consider these 10 digits as a contiguous sequence.
Thanks for suggestions.
Try this:
([\d] *){10}
(That's my whole answer but I have to write some more.)
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I am parsing text and need to detect if any string contains any character NOT including A-Z, 0-9, full stop, comma, plus, minus, or any number of spaces.
I tried the regex expression: "[^A-Z0-9][^.][^,][^-][^+][\S+]"
as well as variations on this, which does not work correctly.
Examples of permissible strings:
1 23842U 96021A 15170.20596865 .00000124 00000-0 00000+0 0 9998
2 23842 0.0589 306.1344 0002868 147.0577 292.5546 1.00269795 70198
Invalid string:
1 2%8!2U 96021A 15170.20596865 .00000124 ^00000-0 00000+0 0 9998
Seems like you want to allow, spaces, alphabets, digits, dot, plus, minus.
Pattern p = Pattern.compile("^[A-Za-z,.+\\s\\d-]+$");