Extract int from a string
Hi,
I am curious if there is an easy way to extract an int value from a String.
For example, "Rb23" to get 23. The length of the part in front of the integer is variable.
Thanks
this seems to work:
public int extract(String s) {
String result = "";
char c[] = s.toCharArray();
int j = c.length-1;
if (!Character.isDigit(c[j])) {
return 0;
do {
result+=c[j--];
} while (j > 0 && Character.isDigit(c[j]));
return Integer.parseInt(new StringBuffer(result).reverse().toString());
}it will return 0 if no numbers are found at the end of the string
Similar Messages
-
Extract numbers from mixed string.
How do I extract numbers from a mixed-character string?
These are house numbers and I have some exceptions - examples below.
How do I get rid of the letter/dash part, and keep the numeric part only?
38A
5600B
23-A
Thanks a lot.
-JohnIt does not work if you have a string like 3343S##@1~!234
OP, you can try this:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> with t as(
2 select '38A' c1 from dual union all
3 select '5600B' from dual union all
4 select '5Bas@#$%1SDf3ff`~7' from dual union all
5 select '3343S##@1~!234' from dual union all
6 select '23-A+9' from dual )
7 --
8 select c1,regexp_replace(c1,'[^[:digit:]]') only_num from t;
C1 ONLY_NUM
38A 38
5600B 5600
5Bas@#$%1SDf3ff`~7 5137
3343S##@1~!234 33431234
23-A+9 239
SQL> -
Extracting data from a string of characters
Hi,
I have a problem that I am having difficulty solving, if anyone has any bright ideas it would be a great help.
I have a description field in one of my tables and the description would read something like
my website can be found at www.mywebsite.com, and it is great
I want to be able to extract the web address from this sentance, so the result would be
www.mywebsite.com
I can do this specific for one sentance but I can't find a way to do it for many different sentances.
cheers in advanceIf you're using 10g, you probably want to do something with regular expressions:
SQL> with strings as (
2 select
3 'my website can be found at www.mywebsite.com, and it is great' str
4 from
5 dual
6 union all
7 select
8 'my website can be found at WWW.myWebSite.com, and it is great'
9 from
10 dual
11 union all
12 select
13 'our web address is www.mywebsite.co.uk' str
14 from
15 dual
16 union all
17 select
18 'http://www.mywebsite.com/great'
19 from
20 dual)
21 select
22 s.str
23 , regexp_substr(s.str, 'www\.[a-z0-9-\.?]*', 1, 1, 'i') extr
24 from
25 strings s
26 /
STR EXTR
my website can be found at www.mywebsite.com, and it is great www.mywebsite.com
my website can be found at WWW.myWebSite.com, and it is great WWW.myWebSite.com
our web address is www.mywebsite.co.uk www.mywebsite.co.uk
http://www.mywebsite.com/great www.mywebsite.comYou'd be better asking this kind of thing on the PL/SQL (and definitely better asking someone other than me anything further about regular expressions!) -
select * from v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for Linux: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - ProductionI want to extract xyz_xyz,abc_abc words from the string ' select * from TEMP_TABLE where trunc(xyz_xyz) = ''xyz'' and trunc(abc_abc) = ''abc'') '
i have tried with this query...
select regexp_substr('select * from TEMP_TABLE where trunc(xyz_xyz) = ''xyz'' and trunc(abc_abc) = ''abc'')', 'trunc[^'''']+''') from dual
can some one help me on this?
Thanks,
MikeHi, Mike,
Mike wrote:
I apologize for this..
create table TEMP_TABLE ( col1 varchar2(10),col2 varchar2(10));
insert into TEMP_TABLE values(' xyz ','abc ');
insert into TEMP_TABLE values(' xyzxyz ','abcabc ');
insert into TEMP_TABLE values(' xyz123 ','abc546 ');
insert into TEMP_TABLE values('xyz','abc');
commit
select * from TEMP_TABLE where trim(col1) ='xyz' and trim(col2) ='abc'
desired output
col1,col2
So the correct output is one row, containing these 9 characters
col1,col2which do not appear in the table. is that right?
How is that output related to the data that is in the table? Do you want that output to appear if there are any rows where TRIM (col1) = 'xyz' and TRIM (col2) = 'abc' (for example, either of these two rows from you sample data:
insert into TEMP_TABLE values(' xyz ','abc ');
insert into TEMP_TABLE values('xyz','abc');), and would you want the result set to contain no rows if the table contained no row like that?
If so,
SELECT DISTINCT
'col1,col2' AS desired_output
FROM temp_table
WHERE TRIM (col1) = 'xyz'
AND TRIM (col2) = 'abc'
;I hope this answers your question.
I have a feeling that it doesn't, or perhaps you have more than one question, since this your earlier messages seemed to have something to do with locating text that was surrounded by single-quotes, and the sample data you posted doesn't contain any single-quotes. -
I have to get the user to input a name and five int's (scores for a test) using a scanner
so it would look like this "Casey 98 96 95 84 92"
how would I pick the integers out of that?This is what the assingment says
Repeatedly (i.e. in a loop) process the input file, placing the results into the output file in a formatted way. In particular:
Read the student name and five exam scores for that student from the input file. You may assume that each line of the file starts with a String representing the student's (last) name, followed by whitespace followed by the five exam scores, each separated by whitespace. Your program does not have to handle the case where there are fewer that six items in the line or the case where the information in the line is not in the order specified.
Does that change anything? -
Help: how to extract digits from a string?
Hi,
I am trying to make a formatted phone number report. The field is VARCHAR2(20). There might be some sort of irregularities. What I need to do are in two steps:
1. extract all digits in order;
2. format the output as (xxx) xxx-xxxx
Any suggestions?
Thank you in advance.
JimmyThis might be of some use to clean up the phone numbers.
drop table phone_numbers;
create table phone_numbers
(phone varchar2(20));
insert into phone_numbers(phone) values ('(111)-555-1212');
insert into phone_numbers(phone) values (' 111 555 1212');
insert into phone_numbers(phone) values ('#555-1212');
insert into phone_numbers(phone) values ('Bob @ 555-1212');
commit;
select phone, translate(phone,'0123456789()-.@#* ','0123456789') edited_phone
from phone_numbers;
PHONE EDITED_PHONE
(111)-555-1212 1115551212
111 555 1212 1115551212
#555-1212 5551212
Bob @ 555-1212 Bob5551212You can see by the last example that the search string in the translate function should contain every character you can type on a keyboard. -
How to extract substring from a string based on the condition ??
Hi,
I'm having a very large string which as below
EQD+CN+SAMPLE18767+2200+++5'
NAD+CA+FIR:172:20'
DGS+IMD+3.2+2346+55:CEL'
FTX+AAA+++GOOD'
FTX+AAA+++ONE'
EQD+CN+SAMPLE18795+2200+++5'
NAD+CA+TIR:172:20'
DGS+IMD+3.2+2346+55:CEL'
FTX+AAA+++SECOND'
FTX+AAA+++IS FAIR'
similarly FTX+AAA as above and it goes on
i tokenized each segment with delimiter as ' and able to read each segment.
Now i want to concatenate the FTX+AAA in a single segment if more than one FTX+AAA with IMMEDIATE below
The output is as follows
EQD+CN+SAMPLE18767+2200+++5'
NAD+CA+FIR:172:20'
DGS+IMD+3.2+2346+55:CEL'
FTX+AAA+++GOOD,ONE'
EQD+CN+SAMPLE18795+2200+++5'
NAD+CA+TIR:172:20'
DGS+IMD+3.2+2346+55:CEL'
FTX+AAA+++SECOND,IS FAIR'
similarly FTX+AAA should be concatenated if it has similar FTX+AAA IMMEDIATE below.
The FTX+AAA segments can come any number of times immediate below
Please help me how we can do this??? Can anyone help me with the code snippet to do this?
Thanks,
KathirEncephalopathic wrote:
You've posted > 300 times here and you still don't respect the rule regarding notification of all cross-posts? [http://www.java-forums.org/advanced-java/30061-how-extract-substring-string-based-condition.html]
Do you think this this will help convince others to help you?See also [http://www.coderanch.com/t/500088/java/java/extract-substring-string-based-condition|http://www.coderanch.com/t/500088/java/java/extract-substring-string-based-condition]. -
How do I extract doubles from a string??
If I have an array of string such as ("4.5 6.2 4.2"). Each array element has either 1, 2 or 3 numbers but they are all in the string format.
I'm looking for advice on how to segment out and then wrap each double.
Any suggestions??If I have an array of string such as ("4.5 6.2 4.2").
Each array element has either 1, 2 or 3 numbers but
they are all in the string format.
String str = "4.5 6.2 4.2";
StringTokenizer tkn = new StringTokenizer(str, " ");
double doub = new double[tkn.countTokens()];
int j=0;
while(tkn.hasMoreTokens() ){
doub[j]= Double.parseDouble(tkn.nextToken() );
I'm looking for advice on how to segment out and then
wrap each double.Good idea
Any suggestions??Yep! -
How to substract an int from a String?
Hi,
Here is my problem,
I have to substract 4 integers out of a single string (in a single input using the JOptionPane.showInputDialog)
these 4 integers are seberated by spaces (any number of spaces)
I have to sign each number to an int variable
also I have to multiply these 4 integers with doubles to get a final double variable result
I need urgent help please
(hint: it's about string manuplation)
ThanksHi,
Here is my problem,
I have to substract 4 integers out of a single string
(in a single input using the
JOptionPane.showInputDialog)
these 4 integers are seberated by spaces (any number
r of spaces)
I have to sign each number to an int variable
also I have to multiply these 4 integers with doubles
to get a final double variable result I have no idea what you're asking. Can you clarify? Maybe provide some sample input and what the output will be.
I need urgent help please I promise you, nobody here cares about your urgency, and mentioning it is guaranteed NOT to get you help any faster. If it has any effect at all, it will be the opposite--some people might decide to delay or skip answering your question simply out of irritation at you mentioning your urgency.
(hint: it's about string manuplation)Yeah. We got that, thanks. -
Int from a string??
OK, before my contacts dry out & I go blind staring at this, please point out the friging error here.
I have the same baasic code for other numeric conversions but this one does not compile, why does javac not like "Int"?
TestNumericTypes.java:122: cannot resolve symbol
symbol : variable Int
location: class biz.TestNumericTypes
anInt = Int.valueOf(s).intValue();
^
1 error
public static boolean TestanInt (String s, boolean popup)
try
anInt = Int.valueOf(s).intValue();
System.out.println("TestanInt: anInt = " + anInt);
j = true;
catch (NumberFormatException nfe)
if (popup = true) // display on screen
alertuser(" (int)");
j = false;
} // end-catch
return j;
} //close method
//-----------------------------------thanks,
-- HSC --The class is called "Integer". You can look it up in
the API docs:
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/packa
e-summary.html
"anInt = Integer.parseInt(s)" is considered better
style and more efficient than "anInt =
Integer.valueOf(s).intValue()", by the way.thanks for the conversion tip, and the correct class name - sometimes I need another to see silly mistakes like this. -
Extract date from text string - Transact-SQL
Hello,
I have a field in my database with an archived date... (Giampaoli Live Oak, Almonds Archive 09/16/10)
I need to be able to extract the date from this, then perform a datepart function on it... How do I extract the date into it's own value using just SQL?
Thanks,
WesThanks Guys,
This helped me:
select
ID,
NAME,
datepart(month, CONVERT(date, SUBSTRING(YOUR_COLUMN_NAME, patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9]%', YOUR_COLUMN_NAME), 50))) AS MONTHARCHIVED,
datepart(YEAR, CONVERT(date, SUBSTRING(YOUR_COLUMN_NAME, patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9]%', YOUR_COLUMN_NAME), 50))) AS YEAR_ARCHIVED
FROM
TABLEABC
Thanks Shiven:) If Answer is Helpful, Please Vote -
Extract number from long string
I have this ugly string in a column of type VARCHAR2. 34382-172.28.129.212(88:6c:de:32:51:a5)-38065-1279731239826
Is there a way to parse this string into a number (remove all letters and punctuation) when selecting from the db? Thanks for any suggestionsI wasn't sure if I got your requirements right so I wrote a statement with several solutions, in any case if you're using Version 10g or higher, regular expressions are your friends:
WITH t as (SELECT '34382-172.28.129.212(88:6c:de:32:51:a5)-38065-1279731239826' col1
FROM dual
SELECT t.col1
-- solution 1: only the first number
, REGEXP_SUBSTR(t.col1, '^\d+') solution_1
-- solution 2: all numbers (without the IP/Mac-Address)
, REGEXP_REPLACE(t.col1, '((^\d+)-|(-\d+))|.','\2\3') solution_2
-- solution 3: all digits
, REGEXP_REPLACE(t.col1, '\D') solution_3
FROM t
COL1 SOLUTION_1 SOLUTION_2 SOLUTION_3
34382-172.28.129.212(88:6c:de:32:51:a5)-38065-1279731239826 34382 34382-38065-1279731239826 343821722812921288632515380651279731239826 C. -
How can I extract text from a string
Hello,
I have a name column that has both first name and last name joined together as a string. I want to separate them in two different columns as firstname and lastname.
I use Report builder, please help with the query that will separate the string into two diferent columns.
ThanksHi sanjb12001,
Per my understanding that you want to separate a name column which contain the information of "First Name", "Middle Name", "Last Name" into three different column, right?
I have tested on my local environment and we have method to do this, one is do this in the query and Patrick Hurst
have already provide the solution, another method is to create three calculated fields using the split function in the expression to get the three new columns.
I assume you have the format like this in the name fields "FirstName MiddleName LastName", Details information about create the three columns for your reference:
Right click the dataset and select the "Add Calculated Field" to add three fields "FirstName", "MiddleName", "LastName":
Using below expression to get the values for these fields:
FirstName:
=split(Fields!name.Value," ")(0)
MiddleName:
=IIF(split(Fields!name.Value," ").Length=3,split(Fields!name.Value," ")(1),"")
LastName:
=split(Fields!name.Value," ")(split(Fields!name.Value," ").Length-1)
Preview you will got the result:
If you still have any problem, please feel free to ask.
Regards
Vicky Liu -
More efficient way to extract number from string
Hello guys,
I am using this Regexp to extract numbers from a string, and I doubt that there is a more efficient way to get this done:
SELECT regexp_replace (regexp_replace ( REGEXp_REPLACE (' !@#$%^&*()_+= '' + 00 SDFKA 324 000 8702 234 | " ' , '[[:punct:]]',''), '[[:space:]]',''), '[[:alpha:]]','') FROM dual
{code}
Is there a more efficient way to get this done ?
Regards,
FatehOr, with less writing, using Perl syntax \D (non-digit):
SELECT regexp_replace(' !@#$%^&*()_+= '' + 00 SDFKA 324 000 8702 234 | " ','\D')
FROM dual
REGEXP_REPLACE(
003240008702234
SQL>
{code}
SY. -
Extract date from string in T-SQL
Hi,
I am having one field(AT_TEXT) in my database that has below kind of values. I want to extract date from this string but the issue is all rows has different format.
1)One Month Lttr sent on 3/12/2009 due to
2)One Month Letter sent on 1/15/2009 due to
3)One Month letter sent on7/15/2011.The amount due - This has no space between on and 7 and . after yr
4)One Month letter sent on 7/16/12 due to - This has 12 instead of 2012(2 positions after 2nd slash/)
Can anyone please help me to write query to extract date from this string?
VickyHi Jingyang
Li,
I checked your query and it looks good but I have one issue. So when we reverse it, it looks like below and it is talking that $57.80 as a 1st numeric value and try to convert it into date and giving me error.
I realize from your query that you are trying to reverse the value and then try to find out 1st Numeric value from that string, but my whole string has another numeric value at the end, like
"One Month Letter on 1/15/2009 due to non-pmt of prems-amt to avoid DE is $57.80"
So when we reverse it, it looks like below and it is taking that $57.80 as a 1st numeric value and try to convert it into date and giving me error.
"08.75$ si ED diova ot eud tma - smerp fo tmp-non ot eud 9002/51/1 no retteL htnoM enO"
Also sometimes I have $ amount at the end and sometimes I don't have any $ amount at the end, like below,
"One Month letter sent on 10/15/2012 due to non-payment of premiums. The amount due to avoid dise"
Can you please modify your query which takes only date part not the $ amount and give me?
Thank you so much for your help
Vicky
Maybe you are looking for
-
My iPad2 "could not be found on the network" and won't sync
Please Help! My iPad 2 won't sync and doesn't seem to be "seen" by my PC. I have rebooted, restored and reset the iPad, and installed latest ios. PC is running Windows 8 with latest iTunes whic has been re-installed. The USB port and cable work fin
-
Can an iPhone be update on a PC if previously updated on a MAC?
Can an iPhone that has always been updated on a Mac be updated on a PC?
-
Task List Access Manager Role in Shared Services
Hi The documentation says this role "Assigns task lists and tasks to other users". I have assigned this role to a group (in Shared Services), I have given that group Manage and Assign access to the Task List (in Planning), and have even done a securi
-
Javascript needed for multiple lines of the same calculations: price x quantity = total
I have received some help already but I need more details - like exactly where to place the code - and - do I place the code before using "Place Multiple Fields? Also, does the first Total1.0 field need any calculation in it or does the area under t
-
Hi there, We're running AireOS 8.0 in a WLC 8500 series and we're getting problems trying to enable HA scenario. These are the scenarios we have tested: management interface tagged + switchport trunk tagged + HA tagged + switchport trunk tagged = SS