0

I have following method

 public void DivideIntoSubStrings(string msg, string methodName, string  userId)
 {   
    string st = String.Format("{0}.{1}.{2}", msg, methodName, userId);
    DoSomething(st);
 }

when I call the following method in the method above and want to get msg, methodName and userId parameters as such

 public void DoSomething(string bigString)
 {
       string st1 = bigString;
       string[] st2 = st1.Split('.');
       string t1 = st2[st2.Length - 1];
       string t2 = st2[st2.Length - 2];
       Console.WriteLine(t1);
       Console.WriteLine(t2);

 }

I'm always sure to get methodName and userId as they were passed into DivideIntoSubString(....) method but msg is a message parameter which can have anything typed into it or in other word I don't know the format of the msg string before hand as it will be passed on during the run-time. It can have commas, periods, underscores etc. So what would be a good idea to get the exact same value with same format of msg parameter as it was passed during the run-time.

4
  • encode message as Base64 before saving Commented Mar 10, 2016 at 17:20
  • 2
    Why don't you just pass the three Strings along separately? Commented Mar 10, 2016 at 17:22
  • 1
    Why are you concatenating the strings before passing them into DoSomething if that is causing problems? Why not just keep all the strings separate? Commented Mar 10, 2016 at 17:22
  • Admiral and Dan I can do what you guys are saying and it makes perfect sense. But it's an old code written many many years back and they way these three parameters are passed to DoSomething(...) as a string and then put together into oracle database in a single column but I have been asked not to tweak any of the existing code just make changes inside DoSomething(..) and then split the whole string into three respective arguments exactly as they were passed and insert all of them into three different oracle columns in an oralce table. Commented Mar 11, 2016 at 1:52

4 Answers 4

2

One tricky way to do it is reversing bigString string, split the string using the overload with the maximum number of elements:

string st1 = new string(bigString.Reverse().ToArray());    
string[] st2 = st1.Split(new char[] { '.' }, 3);

string user = new string(st2[0].Reverse().ToArray());
string method = new string(st2[1].Reverse().ToArray());
string msg = new string(st2[2].Reverse().ToArray());

Console.WriteLine(user);
Console.WriteLine(method);
Console.WriteLine(msg);

Note you need to reverse the splitted strings again to gets the originals

Sign up to request clarification or add additional context in comments.

Comments

2

You can use string.Join to combine the splitted parts of msg together again:

public void DoSomething(string bigString)
{
   string st1 = bigString;
   string[] st2 = st1.Split('.');
   string t1 = st2[st2.Length - 1];
   string t2 = st2[st2.Length - 2];

   string msg = string.Join(".", st2.Take(st2.Length-2));    

   Console.WriteLine(t1);
   Console.WriteLine(t2);
   Console.WriteLine(msg);
}    

The LINQ method Take returns a sequence that represents only the first st2.Length-2 elements of st2.
string.Join joins all the strings in that sequence together by delimiting them again with a period.

2 Comments

I don't think this solves the problem. They are combining strings with a delimiter which is then used in doSomething. Although they wouldn't have to do the length manipulation in doSomething they still cannot reliably split again with .
@AdmiralAdama I updated my answer to show where I wanted to do that. I guess OP's code is a shortened example of what is really happening. I'm sure my code works reliably at this point, though I agree that it might not be an optimal solution for what OP is actually trying to do..
0

please try that following code:

public static void DivideIntoSubStrings(string msg, string methodName, string userId)
{
    string lengths = String.Format("{0}%{1}%{2}", msg.Length, methodName.Length, userId.Length);
    string st = String.Format("{0}.{1}.{2}.{3}", lengths, msg, methodName, userId);
    DoSomething(st);
}

public static void DoSomething(string bigString)
{ 
    string lengthsString = bigString.Split('.').ElementAt(0);
    List<string> lengthsStringArray = lengthsString.Split('%').ToList();
    List<int> lengthIntArray = new List<int>();
    for (int i=0; i < lengthsStringArray.Count; i++)
    {
        lengthIntArray.Add(int.Parse(lengthsStringArray.ElementAt(i))); 
    }
    string msg = bigString.Substring(lengthsString.Length + 1, lengthIntArray.ElementAt(0));
    string methodName = bigString.Substring(lengthsString.Length + msg.Length + 2, lengthIntArray.ElementAt(1));
    string userId = bigString.Substring(lengthsString.Length + msg.Length + methodName.Length + 3, lengthIntArray.ElementAt(2));

    Console.WriteLine(msg);
    Console.WriteLine(methodName);
    Console.WriteLine(userId);
}

You need to know string length, then you can keep it in the string you passed. In this way you'll never care about the join char you choose!

Comments

0

I think this should just be modified.

public void DivideIntoSubStrings(string msg, string methodName, string  userId)
{   
   string[] st = new string[3] { msg, methodName, userId };
   DoSomething(st);
}

And

public void DoSomething(string[] params)
{

}

Or you could just pass it as a Tuple<string, string, string>. Or just pass in msg, methodName, and userId as three parameters to your method if you don't want to store in one variable. There's no reason to concatenate everything only to immediately split it again.

However, if you really want to do that...

If no one can pass in new lines (the source is a single line textbox, for example) might I suggest using {0}\r\n{1}\r\n{2} as your format, instead of something so global as a .?

Then you can use

bigString.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

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.