-1

I want to split this String :

StringA:StringB!StringC.StringD

into:

Array [StringA, :, StringB, !, StringC, ., StringD]

How can i do it in Java and c#?

Thank for your helping!

Minh Le
  • 261
  • 4
  • 14
  • 1
    C#: `var myArray = "StringA:StringB!StringC.StringD".Split(':', '.', '!');` - java: I have no idea. you probably need to import some apache library or other random third party stuff and do `com.sun.Java.retarded.Naming.Convention.Util.StringUtil.Split(theString, new char[]{..});` or something like that. – Federico Berasategui Apr 03 '14 at 02:29
  • @LuiggiMendoza you're right. See [this answer](http://stackoverflow.com/a/3143036/643085) which uses Extension Methods and the `yield` keyword (modern language features which java of course lacks). – Federico Berasategui Apr 03 '14 at 02:57
  • @HighCore I don't know if Java lacks of this *features*, but looks like too much work when you can do a simple lookup through the characters in the array, which obviously all programming languages provide but is up to the developer to use it or to do lot of useless work... and defending *my language should be better for having useless stuff you don't need in yours* :) *clap clap* – Luiggi Mendoza Apr 03 '14 at 14:14
  • @LuiggiMendoza "lot of useless work" - except you didn't realize that's an extension method... sorry, I won't discuss with you here, cause it's offtopic in SO. [java sucks](http://whyjavasucks.com/). BTW, 10 lines of code don't look like "too much work" for me. The fact that you don't understand the code because it has several language level features you never saw before doensn't mean it's "too much work". – Federico Berasategui Apr 03 '14 at 14:57
  • @HighCore it is off topic for the discussion, but as I pointed to you months ago, if you don't like it, it is fine. But do not think saying *java doesn't work for me like C# does* would make the world stop and saying *oh yes you're completely right, we should drop Java technology and do everything in C# from now on*. Just stop complaining about your bad luck or your bad time with it and try to **help people**. – Luiggi Mendoza Apr 03 '14 at 15:00
  • @HighCore is not that I don't understand the code, is that I understood it so I can say: that piece of code does lot of things you don't really need to solve the problem. Look, I can do this using C, a `char*` and a `for` loop, because that's what you need. If you want to add fancy stuff to *make it look better* it's up to you. – Luiggi Mendoza Apr 03 '14 at 15:02
  • @luiggimendoza can you name what exactly the code does that "is not needed"? – Federico Berasategui Apr 03 '14 at 15:04
  • @luiggimendoza BTW, your `for` loop solution in C is also "too much unneeded code", compared to [this other answer](http://stackoverflow.com/a/521172/643085). – Federico Berasategui Apr 03 '14 at 15:09
  • @HighCore that regex is more expensive in terms of performance... – Luiggi Mendoza Apr 03 '14 at 15:10
  • @luiggiMendoza right, because doing a regex **1 time** over a **20 character long** string is going to cause a **modern** CPU (in 2014) to choke to death. **Premature optimization is the root of all evil.** – Federico Berasategui Apr 03 '14 at 15:12
  • @HighCore you won't know until you test it... and is a plain simple problem. – Luiggi Mendoza Apr 03 '14 at 15:13
  • @luiggimendoza sorry you're still programming for 80386 class CPUs. Here, we require our customers to have real computers. – Federico Berasategui Apr 03 '14 at 15:15
  • @HighCore our customers also have real computers, but we have performance as our priority quality attribute, so what you may think it is a *premature optimization* is for us the solution. Anyway, you had turn this into a personal problem and you forgot the real problem: help OP. By the way, next time at least vote to close using a real reason instead of voting for *too broad* when this question obviously isn't =\. – Luiggi Mendoza Apr 03 '14 at 15:18

2 Answers2

3

Try this using Java,

        String value = "StringA:StringB!StringC.StringD";
        char[] charArray = value.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();

        for (char out : charArray)
        {
            if (!Character.isLetterOrDigit(out))  // find special characters
            {
                stringBuilder.append(",").append(out).append(","); 
            }
            else
            {
                stringBuilder.append(out);
            }
        }

        String[] resultValue = stringBuilder.toString().split(",");
        System.out.println(Arrays.toString(resultValue));
newuser
  • 8,338
  • 2
  • 25
  • 33
0

C#

you can use String.Split(char)

try this one

http://msdn.microsoft.com/en-us/library/b873y76a.aspx