3

I need to read only specific portions of a complete string. The string would be in the form of:

"1 Some Currency Name = 0.4232 Other Currency Name"

So, the quickest method I could come up with was to split the string at the equal (=) operator thus adding two values to the array, like so:

string rawInput = "1 Some Currency Name = 0.4232 Other Currency Name";
string[] rawSplit = rawInput.Split('=');
string firstRate = rawSplit[0].ToString();
string secondRate = rawSplit[1].ToString();

I now need to get only the first part of the secondRate string ("0.4232"). So I would split that string again (bad coding):

string[] lastSplit = secondRate.Split(); //Split at whitespace characters
string firstValue = lastSplit[0].ToString(); //Should return "0.4232" but instead returns ""

When I run the application to test this function, it returns an empty string instead of the value "0.4232". Why is this happening? What am I missing here?

Complete Method:

private void btnTest_Click(object sender, EventArgs e)
{
    string rawInput = "1 Some Currency Name = 0.4232 Other Currency Name";
    string[] rawSplit = rawInput.Split('=');
    string baseRate = rawSplit[0].ToString(); //1 Some Currency Name
    string conversionRate = rawSplit[1].ToString(); //0.4232 Other Currency Name

    rawSplit = GetSplit(conversionRate);
    XtraMessageBox.Show(rawSplit[0].ToString()); //Returns blank string here???
}

private string[] GetSplit(string inputString)
{
    return inputString.Split();
}

Any ideas or suggestions welcome and will be greatly appreciated!

2
  • If you split at = then the string will be ` 0.4232...` which means the first entry is empty when splitting by whitespace. Commented Aug 29, 2012 at 19:59
  • 2
    Out of interest, why are you calling ToString() on values which are already strings? Commented Aug 29, 2012 at 20:00

5 Answers 5

15

The problem is, when you split your original string at '=', you're getting a result with an extra space at the beginning in the result, basically rawSplit[1] is equal to " 0.4232 Other Currency Name".

You can use the overload of String.Split that allows you to specify to remove empty entries, which will trim off any blank entries in the result.

string[] lastSplit = secondRate.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

On a side note, there is no need to use ToString() when the input is already a string:

string[] rawSplit = rawInput.Split('=');
// rawSplit is a string array, so rawSplit[0] is already a string...
string firstRate = rawSplit[0]; // .ToString();
Sign up to request clarification or add additional context in comments.

Comments

1

Your current problem can be solved as already been answered by Reed Copsey

new[] {' '}

However, I am showing some other ways of doing so

By using Regular Expression

var result = Regex.Match(secondRate, @"\d+(\.\d+)?").Value;

But suppose your input string is of type "0.4232 Other Currency Name 0.45" where you need to match both the decimal values. In that case you can go ahead with Regex.Matches

e.g.

string secondRate ="0.4232 Other Currency Name 0.45";
var result = Regex.Match(secondRate, @"\d+(\.\d+)?").Value;

            MatchCollection matches = Regex.Matches(secondRate, @"\d+(\.\d+)?");
            foreach (Match match in matches)
            {
               foreach (Capture capture in match.Captures)
               {
                   Console.WriteLine("Value={1}", capture.Index, capture.Value);
               }
           }

By Using Where extension methods

Complete code

string rawInput = "1 Some Currency Name = 0.4232 Other Currency Name 0.45";
            string[] rawSplit = rawInput.Split('=');
            string firstRate = rawSplit[0].ToString();
            string secondRate = rawSplit[1].ToString();
            string[] lastSplit = secondRate.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            decimal num = 0;
            lastSplit
                .Where(d => decimal.TryParse(d, out num))
                .ToList()
                .ForEach(i => Console.WriteLine(i));

Output

0.4232
0.45

Comments

0

The below will return the string " 0.4232 Other Currency Name"

string conversionRate = rawSplit[1].ToString();

This has a space before it so when split again at:

return inputString.Split();

The 0 position in the array is that space, you either want position 1 or do a trim before that final split.

Comments

0

The second part of string is = 0.4232, There is a whitespace between = and 0.4232. So when you split, the first entry is empty string. You can either check the second entry, but better way would be to string.Trim() before using string.Split()

Comments

0

As stated before by other users, the problem is the white space. One simple solution would be adding trim() to conversionRate:

rawSplit = GetSplit(conversionRate.Trim());

That will allow to keep the rest of your code intact (even so, it would be advisable to take out the unnecessary use of ToString()).

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.