I am having trouble splitting a string when a character is found. I know how to split strings when it is in an array. But I don't know how to split a string when it is passed as a command line argument. This is a string argument that gets passed in and I have to add spaces when the bitwise Or
is found and also when the colon
is found I have to add a new line. I don't really know how to approach this problem when it gets passed as a argument. Any help would be awesome thanks.
"Tassimo T46 Home Brewing System|43-0439-6|17999|0.30:Moto Precise Fit Rear"+
"Wiper Blade|0210919|799|0.0: Easton Stealth Reflex Composite Hockey Stick|"+
"83-4567-0|8999|0.5:Yardworks 4-Ton Log Splitter|60-3823-0|39999|0"
/**
<P>{@code java SplitXmpl}</P>
**/
public class SplitXmpl {
public static final void main(String[] igno_red) {
String sInput = "Tassimo T46 Home Brewing System|43-0439-6|17999|0.30:Moto Precise Fit Rear Wiper Blade|0210919|799|0.0: Easton Stealth Reflex Composite Hockey Stick| 83-4567-0|8999|0.5:Yardworks 4-Ton Log Splitter|60-3823-0|39999|0";
String sOutput = sInput.replaceAll("\\|", " ").replaceAll(":", System.getProperty("line.separator", "\n"));
System.out.println(sOutput);
}
}
Output:
[C:\java_code]java SplitXmpl
Tassimo T46 Home Brewing System 43-0439-6 17999 0.30
Moto Precise Fit Rear Wiper Blade 0210919 799 0.0
Easton Stealth Reflex Composite Hockey Stick 83-4567-0 8999 0.5
Yardworks 4-Ton Log Splitter 60-3823-0 39999 0
Another possible solution:
public static void main(String[] args) {
String s = args[0];
s = s.replace("|", " ").replace(":", "\n");
System.out.println(s);
}
Run with:
java Main "Tassimo T46 Home Brewing System|43-0439-6|17999|0.30:Moto Precise Fit Rear Wiper Blade|0210919|799|0.0: Easton Stealth Reflex Composite Hockey Stick| 83-4567-0|8999|0.5:Yardworks 4-Ton Log Splitter|60-3823-0|39999|0"
Command line arguments is nothing but a String array. So you can work on them just like any other String object. Have a look at the String API for the functionality you are trying to implement, the replace() method should suffice for you.
The above answers are correct if the following assumptions are used
asasa asaas assaa
has three argument and asasaasaasassaa
has only 1 argument for the first case the above answers can work but for the second case the following code snippet works
INPUT
Tassimo T46 Home Brewing System|43-0439-6|17999|0.30:Moto Precise Fit Rear Wiper Blade|0210919|799|0.0: Easton Stealth Reflex Composite Hockey Stick| 83-4567-0|8999|0.5:Yardworks 4-Ton Log Splitter|60-3823-0|39999|0
public class test {
public static void main(String[] args) {
int l=args.length;
StringBuilder builder=new StringBuilder();
while(l-->0){
builder.append(args[l]);
}
System.out.println(builder.toString().replace("|", " ").replace(":","\n"));
}
}
OUTPUT
Splitter 60-3823-0 39999 0Log4-Ton83-4567-0 8999 0.5 YardworksStick HockeyCompositeReflexStealthEastonBlade 0210919 799 0.0 WiperRearFitPreciseSystem 43-0439-6 17999 0.30 MotoBrewingHomeT46Tassimo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.