Handle String in JAVA?

2

I have a query of SQL that the content brings me like this:

[email protected];[email protected]

I need only the first address, I have to disregard everything that is from ; ?

    
asked by anonymous 03.04.2017 / 20:35

3 answers

2

If it goes directly to SQL use a function join, SUBSTRING and CHARINDEX :

SELECT SUBSTRING(email, 1,CHARINDEX(';', email)-1) from emails;

where email is the field that stores the electronic addresses separated by commas and emails the name of the table.

Retired response: < sub> SoEn - How to split a comma-separated value to columns

A suitability for picking up emails that do not have items separated by ; , that is, only an email contained in the field without ; :

select CASE WHEN Charindex(';', email)>0 
   THEN Substring(email, 1,Charindex(';', email)-1) ELSE
   email END
from emails

03.04.2017 / 21:04
1

There are a few ways:

with indexOf

String result = "[email protected];[email protected]";
int index = result.indexOf(";");
if (index > 0) {
    result = result.substring(0, index);
}
System.out.println(result);

with split

String result = "[email protected];[email protected]";
String[] emails = result.split(";");
result = emails[0];
System.out.println(result);
    
03.04.2017 / 20:47
1

You can use the Java split function to do this.

String email = "[email protected];[email protected]";
String[] emails = email.split(";"); 
System.out.println(emails[0]);

The split function divides a String into several Strings using the given delimiter and returns the parts in a String vector. As you need only the first, just use:

emails[0]

See working at ideone .

    
03.04.2017 / 20:48