Adding new value to String array based on condition

user2211290

String Array contains values like {'2','4','8'}

if 8 is present, want to add 999 to it,if string array contains 999 want to add 8 to it.

If both 8 and 999 are present in array no need to add any value to array.

cramopy

Have a look at the following:

public string[] Add8or999(string[] source)
{
    string[] output = source;
    if (source.Contains("8") && source.Contains("999"))
    {
        // what to do here? 
    }
    else if (source.Contains("8"))
    {
        output = new string[source.Length + 1];
        for (int i = 0; i < source.Length; i++)
        {
            output[i] = source[i];  
        }
        output[source.Length] = "999";
    }
    else if (source.Contains("999"))
    {
        output = new string[source.Length + 1];
        for (int i = 0; i < source.Length; i++)
        {
            output[i] = source[i];  
        }
        output[source.Length] = "8";
    }
    return output;
}

Basic Usage:

string[] s = Add8or999(new string[] {"8", "9", "10"});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Adding value to cell based on ending of string in another cell in Excel

分類Dev

Get min/max value from array based on condition

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

Creating a new array of numbers based on an existing string array

分類Dev

Exception of Adding a new value on Regedit

分類Dev

Adding new line to here string

分類Dev

Adding new element to array in MongoDb

分類Dev

Lookup for a value in column based on condition

分類Dev

Filter javascript array based on condition

分類Dev

Adding condition (string) to Entity LINQ query

分類Dev

Vue.js trying to increment array value when adding new row

分類Dev

adding new rows based on column header

分類Dev

How to create a new column based on condition?

分類Dev

create new column in pandas based on condition

分類Dev

Trouble adding new value to List Object

分類Dev

Adding array object to an array in a foreach loop if a condition is satisfied in php

分類Dev

Emberjs - adding new record to existing array

分類Dev

Assign a value to column based on condition across rows

分類Dev

Replace matched patterns in a string based on condition

分類Dev

Replacement of an element based upon condition within an array

分類Dev

How to shorten an array based on condition in bash

分類Dev

Making a new array based on an exploded array

分類Dev

Mapping array of objects into new array of array by value

分類Dev

Split a string based on ASCII value

分類Dev

split array based on value php

分類Dev

Create an array of a Class based on string

分類Dev

Why does adding an array to number return a string?

分類Dev

Extract a value from an object in array matching a condition

分類Dev

New column based on 1 condition using index and one column groupby

Related 関連記事

  1. 1

    Adding value to cell based on ending of string in another cell in Excel

  2. 2

    Get min/max value from array based on condition

  3. 3

    javascript return property value from nested array of objects based on condition

  4. 4

    Creating a new array of numbers based on an existing string array

  5. 5

    Exception of Adding a new value on Regedit

  6. 6

    Adding new line to here string

  7. 7

    Adding new element to array in MongoDb

  8. 8

    Lookup for a value in column based on condition

  9. 9

    Filter javascript array based on condition

  10. 10

    Adding condition (string) to Entity LINQ query

  11. 11

    Vue.js trying to increment array value when adding new row

  12. 12

    adding new rows based on column header

  13. 13

    How to create a new column based on condition?

  14. 14

    create new column in pandas based on condition

  15. 15

    Trouble adding new value to List Object

  16. 16

    Adding array object to an array in a foreach loop if a condition is satisfied in php

  17. 17

    Emberjs - adding new record to existing array

  18. 18

    Assign a value to column based on condition across rows

  19. 19

    Replace matched patterns in a string based on condition

  20. 20

    Replacement of an element based upon condition within an array

  21. 21

    How to shorten an array based on condition in bash

  22. 22

    Making a new array based on an exploded array

  23. 23

    Mapping array of objects into new array of array by value

  24. 24

    Split a string based on ASCII value

  25. 25

    split array based on value php

  26. 26

    Create an array of a Class based on string

  27. 27

    Why does adding an array to number return a string?

  28. 28

    Extract a value from an object in array matching a condition

  29. 29

    New column based on 1 condition using index and one column groupby

ホットタグ

アーカイブ