Hi All...
Plz solve my problem.
String temp="Vendor number modified from 12345 to 00056789"
How to separate these two numbers ?
Plz reply soon.
A couple of practical ways to get the two numbers out of that string, depending on whether you can use regex (as suggested) or need a solution that works on an older Java runtime (which is why pointed to import/JRE issues).
A simple substring approach when the surrounding words are stable:
String s = "Vendor number modified from 12345 to 00056789";
int iFrom = s.indexOf("from ");
int iTo = s.indexOf(" to ", iFrom);
if (iFrom != -1 && iTo != -1) {
String oldNum = s.substring(iFrom + 5, iTo).trim();
String newNum = s.substring(iTo + 4).trim();
} This preserves leading zeros and is very fast, but relies on the exact "from" / "to" wording.
A more robust no-regex scanner that finds any contiguous digit sequences (works on older JDKs):
List numbers = new ArrayList();
StringBuilder cur = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
cur.append(c);
} else if (cur.length() > 0) {
numbers.add(cur.toString());
cur.setLength(0);
}
}
if (cur.length() > 0) numbers.add(cur.toString());
// numbers.get(0) and numbers.get(1) are the values Notes: keep numbers as strings if leading zeros are meaningful; converting with Integer.parseInt/Long.parseLong removes leading zeros and can throw exceptions on overflow (use BigInteger for very large values). If your IDE reports unresolved classes for regex types, check project JRE/compiler compliance or use one of the non-regex methods above.
Jump to Post— Ezzaral 2,714This is easily done with the java.util.regex package.
String temp="Vendor number modified from 12345 to 00056789"; Pattern pattern = Pattern.compile("Vendor number modified from (\\d+) to (\\d+)"); Matcher matcher = pattern.matcher(temp); if (matcher.matches()){ String fromNumber = matcher.group(1); String toNumber = matcher.group(2); System.out.println("from: "+fromNumber); System.out.println("to: "+toNumber); }
Jump to Post— peter_budo 2,532Have you imported java.util.regex.Pattern for Pattern and java.util.regex.Matcher for Matcher or at least imported java.util.regex.*? Defenetly not...
This is easily done with the java.util.regex package.
String temp="Vendor number modified from 12345 to 00056789";
Pattern pattern = Pattern.compile("Vendor number modified from (\\d+) to (\\d+)");
Matcher matcher = pattern.matcher(temp);
if (matcher.matches()){
String fromNumber = matcher.group(1);
String toNumber = matcher.group(2);
System.out.println("from: "+fromNumber);
System.out.println("to: "+toNumber);
} But it's showing Pattern & Matcher unresolved & util.regex package not found.
Have you imported java.util.regex.Pattern for Pattern and java.util.regex.Matcher for Matcher or at least imported java.util.regex.*? Defenetly not...
Hi...
By using java.util.regex.* or java.util.regex package an error msg as symbol can't be resolved is coming.I think it's not supported in WSAD 5.1.What might be the reason ?
Hi...
By using java.util.regex.* or java.util.regex package an error msg as symbol can't be resolved is coming.I think it's not supported in WSAD 5.1.What might be the reason ?
From my searching, it looks like 5.1 uses java 1.3 by default, you need to switch this to java 1.4. This thread has some info:
Your development tool is 4 years out of date and you really should look into a more recent version.
Thnx very much Sir....
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.