简体   繁体   中英

Split long string for each colon “:” and get index of the line by position

im struggling with the understanding of using Split method to receive my desired texts im receiving long registration string from user and im trying to split it by colon : and for each colon found i want to get all the text until /n in the line

The string i'm receiving from the user is formatted like this example:

"Username: Jony \n
 Fname: Dep\n
 Address: Los Angeles\n
 Age: 28\n
 Date: 11/01:2001\n"

Thats my approche until now didnt figurate out how it works and didnt found question similler like my question

str = the long string

List<string> names = str.ToString().Split(':').ToList<string>();
names.Reverse();

var result = names[0].ToString();
var result1 = names[1].ToString();

Console.WriteLine(result.Remove('\n').Replace(" ",string.Empty));
Console.WriteLine(result1.Remove('\n').Replace(" ",string.Empty));

Benchmarks

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
Description      : Intel64 Family 6 Model 58 Stepping 9
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3901 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

Results

--- Random characters -------------------------------------------------
| Value |   Average |   Fastest |   Cycles | Garbage | Test |    Gain |
--- Scale 1 -------------------------------------------- Time 1.152 ---
| split |  4.975 µs |  4.091 µs | 20.486 K | 0.000 B | N/A  | 71.62 % |
| regex | 17.530 µs | 14.029 µs | 65.707 K | 0.000 B | N/A  |  0.00 % |
-----------------------------------------------------------------------

Original Answer

You could use regex , or you could simply use Split

var input = "Username: Jony\n Fname: Dep\nAddress: Los Angeles\nAge: 28\nDate: 11/01:2001\n";

var results = input.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Split(':')[1].Trim());

foreach (var result in results)
   Console.WriteLine(result);

Full Demo Here

Output

Jony
Dep
Los Angeles
28
11/01

Note : This has no error checking, so if your string doesn't contain a Colon, it will break


Additional Resources

String.Split Method

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character arr

StringSplitOptions Enum

Specifies whether applicable Split method overloads include or omit empty substrings from the return value

String.Trim Method

Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current String object are removed.

Enumerable.Select Method

Projects each element of a sequence into a new form.

You can use a regex to find the matches after colon and up to the Newline character:

(?<=:)\s*[^\n]*

The regex uses a look back, ensuring there's a colon in front of the string, then it matches everything not being Newline = rest of line.

Use it like this:

string searchText = "Username: Jony\n
 Fname: Dep\n
 Address: Los Angeles\n
 Age: 28\n
 Date: 11/01:2001\n";

Regex myRegex = new Regex("(?<=:)\s*[^\n]*");
foreach (Match match in myRegex.Matches(searchText))
{
    DoSomething(match.Value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM