How to alter Java strings contain only certain alphabetical characters [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
how can I alter text string fields in java to only contain certain alphabetical characters (f,-)
in this format: 2f5-4, 2f6, 8f9
Only numbers f numbers and
numbers f number - numbers

You could use a regular expression to check, if the String in your text field is valid:
\d+f\d+(?:-\d+)?
Java code sample
Pattern pattern = Pattern.compile("\\d+f\\d+(?:-\\d+)?");
for (String s : new String[] {
"2f6", "2f9", "6f10", "5f9-2", "3f4-9"
}) {
System.out.println("String: \""+s+"\" match: "+pattern.matcher(s).matches());
}

Regex expression =>
(([0-9]+)([f])([0-9]*)|(([-])([0-9]*)))\w+

Related

last two occurance of semicolon(;) in given String using Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How do I get the after last two semicolon's occurrences of a string ?
Example:
Mobiles;Students;Test;Yes;1234
The output should be
Yes;1234
Using a regex replacement, we can try:
String input = "Mobiles;Students;Test;Yes;1234";
String output = input.replaceAll("^.*;([^;]+;[^;]+)$", "$1");
System.out.println(output); // Yes;1234

Regex to get the last value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
From the following value
[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]
I want to get the last number 2196879032
Some times value will be -1
[AAMP-PLAYER]aamp pos: [14..187..202..-1]
Instead of split method how can i extract last digit using regex method
You could match the last (possibly negative) number at the end of the string, just before the closing ]:
(\-?\d+)\]$
A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
(\-?\d+)(?!.*\d)
We could try using a String#replaceAll option here:
String input = "[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]";
String num = input.replaceAll("^.*\\.\\.(-?\\d+).*$", "$1");
System.out.println(num);
This prints:
2196879032

How could I cut string strings by words according to a length? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I cut a string string by words according to a length
String text = "Hello world welcome";
The maximum size of a row must be 15 characters, if you used the substring method I would stay like this
Hello wordl wel
come
and it should not be like that, it should be like that
Hello wordl
welcome
Use Apache Commons Text WordUtils.wrap() method:
String out = WordUtils.wrap("Hello world welcome", 15);
System.out.println(out);
will output:
Hello world
welcome

Changing the Lengths of Words in a String - Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am currently writing a game in Java, where words in a string will have to be all changed to equal 5 characters a word.
eg. I am writing in Java
Iamwr iting inJav a
I wonder if anyone knows how I would do this?
First remove spaces between words.
Then split that resulting String to length with 5.
Add a space after every 5 character.

Regular expression for getting values inside brackets [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Below are the String values I want to separate the inside brackets value
US Records (100)
Foreign Records (243)
In the string above I want to separate the counts and store into another string
100, 243 using regular expression.
This code schold give you the result:
String s = new String("US Records (100)");
Pattern p = Pattern.compile("\\((\\d+)\\)");
Matcher m = p.matcher(s);
m.find();
System.out.println(m.group(1));

Categories

Resources