Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Manipulation - Comma Delimited Items

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

Using Java 1.4 I need to perform two tasks on a string variable where part of the items are delimited by commas:
Code:
String sUsersA = "groupa=user1,user2,user3";
1. Determine if a string (say user2) is in sUsersA (true/false).
2. Remove an item from string (say user2) from sUsersA preserving the other items in string. For instance: groupa=user1,user2,user3 would become groupa=user1,user3

I have been scouring the docs I dont quite have it.

What can you recommend?

Thanks,

Michael42

 
Hi

Code:
[maroon]public[/maroon] [red]class[/red] Michael
{
  [maroon]public[/maroon] Michael()
  {
    String sUsersA=[green]"groupa=user1,user2,user3"[/green];
    String searchFor=[green]"user2"[/green];
    String removeIt=[green]"user2"[/green];
    
    System.out.println(sUsersA+[green]" contains "[/green]+searchFor+[green]" : "[/green]+sUsersA.matches([green]".*\\b"[/green]+searchFor+[green]"\\b.*"[/green]));
    System.out.println(sUsersA+[green]" without "[/green]+removeIt+[green]" : "[/green]+sUsersA.replaceFirst(removeIt+[green]",?"[/green],[green]""[/green]).replaceFirst([green]",$"[/green],[green]""[/green]));

[gray]// or[/gray]

    String[] users=sUsersA.substring(sUsersA.indexOf([green]"="[/green])+1).split([green]","[/green]);
    
    [red]boolean[/red] ok=[teal]false[/teal];
    [maroon]for[/maroon] ([red]int[/red] i=0;i<users.length;i++) [maroon]if[/maroon] (users[i].equals(searchFor)) ok=[teal]true[/teal];
    System.out.println(sUsersA+[green]" contains "[/green]+searchFor+[green]" : "[/green]+ok);
    
    String ne=[green]""[/green];
    [maroon]for[/maroon] ([red]int[/red] i=0;i<users.length;i++) [maroon]if[/maroon] (!users[i].equals(removeIt)) ne+=(ne.length()>0?[green]","[/green]:[green]""[/green])+users[i];
    ne=sUsersA.substring(0,sUsersA.indexOf([green]"="[/green])+1)+ne;
    System.out.println(sUsersA+[green]" without "[/green]+removeIt+[green]" : "[/green]+ne);

[gray]// or[/gray]

[gray]// dozen more possibilities[/gray]

  }
}
But this is not your homework, right ?

Feherke.
 
That seems like quite a lot of work. A simpler routine could be :

Code:
public String checkAndRemove(String src, String check, String delim) {
	if (src.indexOf(check) != -1) {
		if (src.indexOf(check +delim) != -1) {		
			src = src.replaceAll(check +delim, "");
		} else {
			src = src.replaceAll(delim +check, "");
		}
	}
	
	return src;
}

Called like :

Code:
String sUsersA="groupa=user1,user2,user3";
String searchFor="user2";
sUsersA= checkAndReplace(sUsersA, searchFor, ",");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi

Excuse me sedj, but I do not understand why your code is shorter then my first solution ? The only difference is that you code does not match Michael42's first requirement. And your code fails if the list contains only one user :
Code:
String sUsersA="groupa=user2";
( By the way, you mistyped the method name... checkAndRemove() in the declaration and checkAndReplace() in the call. )

I wrote the second solution because the regular expression solution would fail for values like :
Code:
String sUsersA=[green]"groupa=user1,use\\r2,user3"[/green];
String searchFor=[green]"use\\r2"[/green];
String removeIt=[green]"use\\r2"[/green];
But today I took a closer look at the Java regular expressions and discovered that they support quoting just like Perl. So the quick and safe way is :
Code:
System.out.println(sUsersA+[green]" contains "[/green]+searchFor+[green]" : "[/green]+sUsersA.matches([green]".*\\b\\Q"[/green]+searchFor+[green]"\\E\\b.*"[/green]));
System.out.println(sUsersA+[green]" without "[/green]+removeIt+[green]" : "[/green]+sUsersA.replaceFirst([green]"\\Q"[/green]+removeIt+[green]"\\E,?"[/green],[green]""[/green]).replaceFirst([green]",$"[/green],[green]""[/green]));
Or the same in sedj's style :
Code:
[maroon]public[/maroon] String checkAndRemove(String src, String check, String delim) {
    [maroon]if[/maroon] (src.indexOf(check) != -1) {
        [maroon]if[/maroon] (src.indexOf(check +delim) != -1) {        
            src = src.replaceAll([green]"\\Q"[/green]+check +delim+[green]"\\E"[/green], [green]""[/green]);
        } [maroon]else[/maroon] {
            src = src.replaceAll([green]"\\Q"[/green]+delim +check+[green]"\\E"[/green], [green]""[/green]);
        }
    }
    
    [maroon]return[/maroon] src;
}

Feherke.
 
My example was shorter than yours, because .... it has less lines.



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top