Regex expression to get the number out of the string [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like follows
XXXX456
For example i sometimes get the string as
<ignore>456
I want to get the number from this string

Try this:
String s = "<ignore>456";
System.out.println(s.replaceAll("\\D", ""));

Try:
yourStr = yourStr.replaceAll("[^\\d]", "");

Try this:
"([0-9]+)$"
Will match any number that's at the end.

Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher("<ignore>456");
while (m.find()) {
System.out.println(m.group());
}

Related

Check for specific string format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How would I check in java if a string entered match the required format of aa/dddd where a represents a lower or upper-case letter and d represents a number
A simple regex to use: ^[A-Za-z]{2}/[0-9]{4}$
String regex = "^[A-Za-z]{2}/[0-9]{4}$";
String test = "ab/1232";
if (test.matches(regex)) {
// my regex matches
}
try this,
String str="aF/1234";
if(str.matches("[a-zA-Z]{2}/\\d{4}"))
System.out.println("Match");
I believe
String.matches("[A-Za-z]{2}/\\d{4}")
or
String.matches("[A-Za-z]{2}/[0-9]{4}")
would work.

How to display a portion of a string at an index of a string array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string array
String[] albumnames;
now how to take a string from particular index position with limited number of charachters.
For example,
if, albumnames[position] have value "abcdefghijk"
then i want to take the first 5 characters only.
That is "abcde".
The substring method of String can be used to achieve this. Try
String s = albumnames[position].substring(0,5);
See substring docs
albumnames[position].substring(0,5);
you can see the methods in String class, like substring, indexof

How to create substring from string at specific fixed length [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have java String with more than 160 character, on that i want to create substring form that string for first 150 character.
I have google it for that but what i got is for make substring at special character.
Please show me possible way for do this..
String#substring(int beginIndex, int endIndex) is what you need.
String mySubString = myString.substring(0, 150);

How can I read a single word from a txt file and make it a variable in my code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a text file with a single word say "fish " but it will have spaces after the word. is there anyway I can pull the word fish as a variable but leave off the spaces?
Scanner in = new Scanner(new File("path_to_file.txt"));
String var = in.next();
var = var.trim();
in.close();
Not the most efficient way, but one of the simplest.
string Text = File.ReadAllText("path_to_file.txt").trim();

Need to get string part between two / in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.

Categories