0

I posted a question over here: Selecting a substring from a string in C# and I got a nice suggestion of using following code:

int index = String2.IndexOf(String1);
if(index >= 0)
{
    string result = String1;
    if (String1.Length < String2.Length)
    {
        string rest = String2.Substring(index + String1.Length);
        var chars = rest.TakeWhile(c => !Char.IsLetter(c) && !Char.IsWhiteSpace(c));
        result = result + string.Join("", chars);
    }
}

I used it in .Net 4.0 and it's working fine. The problem is that I have to use .Net 3.0. Is there any way I can use String.Join in .NET 3.0 or .NET 3.5?

6
  • 2
    Are you sure? System.Join exist since .NET Framework 2.0 as far as I know. What is the values of String2 and String1? What is your output and what do you expect? Commented Mar 5, 2014 at 14:14
  • documentation is a good place to start Commented Mar 5, 2014 at 14:15
  • The TakeWhile is probably the issue. Commented Mar 5, 2014 at 14:16
  • .Net 3.0? I never heard anyone use that. .Net 3.5 is more common, but that will have what you need. If you are missing Linq features take a look at LinqBridge or similar frameworks Commented Mar 5, 2014 at 14:20
  • @SonerGönül link in this they described that String.join does not exist in Dot Net 3.5 Commented Mar 5, 2014 at 14:30

3 Answers 3

1

In .NET 3 there are only two overloads for String.Join, so you need strings and you don't have LINQ.

Another way is not to use String.Join at all but for example a StringBuilder:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < rest.Length; i++)
{
    char c = rest[i];                       
    bool takeChar = !Char.IsLetter(c) && !Char.IsWhiteSpace(c);
    if (takeChar)
        sb.Append(c);
    else
        break;
}
result = result + sb.ToString();
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't that call the String.Join(string, params object[]) one which was introduced in 4.0 too?
Similar functionality, but not exact. The call resulting above goes to String.Join(string, string[]) because of the Select LINQ statement returns string values. The 4.0 call with params does not require a strongly-typed string array, and can take a comma-separated list of objects.
@Dirk: i have edited my answer to use a StringBuilder since LINQ in .NET 3 is also a problem.
0

I don´t know. I will give you a way to do it.

Convert your string in a StringBuilder in the System.Text namespace

Then do something like:

StringBuilder sb = new StringBuilder();
sb.Append(chars);

And pass your chars.

The result will be the same

Comments

0

String.Join does work in .NET versions prior to 4.0 with the exception of the overloads taking an IEnumerable<T> and IEnumerable<string>.

In your code chars is an IEnumerable<char> so the method you use will be unavailable. You can use a different overload, for example

String.Join("", chars.Select(c => c.ToString()).ToArray());

which will use the String.Join(string, string[]) one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.