Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String extraction in C#

Tags:

string

c#

I'm getting pretty frustrated with this, and hope the community can help me out.

I have a string, an example would be "1_ks_Males", another example would be "12_ks_Females".

What I need to do is write a method that extract's each value. So from the first example I'd want something like this:

1 ks Males

In separate variables.

I'm sure I'm just being incredibly thick, but I just can't get it!

like image 807
Paul Avatar asked Jan 24 '12 10:01

Paul


People also ask

Is string slicing possible in C?

The strtok() function is the traditional C library routine used to slice up a string.

How do you extract a string from a string?

You call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string. The starting character position is a zero-based; in other words, the first character in the string is at index 0, not index 1.

What is parsing a string in C?

The strtok function is a handy way to read and interpret data from strings. Use it in your next project to simplify how you read data into your program. By Jim Hall (Correspondent) April 30, 2022 | 0 Comments | 4 min read.

What is Strcpy in C?

strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string. h header file and in C++ it is present in cstring header file. Syntax: char* strcpy(char* dest, const char* src);


2 Answers

Simply use string.Split('_'). With your input strings it will return a string array with three elements.

like image 84
Daniel Hilgarth Avatar answered Oct 04 '22 00:10

Daniel Hilgarth


You can use Split function for String. Something like this

var split =  "1_ks_Males".Split('_');
var first = split[0];
var second = split[1];
var third = split[2];
like image 42
Amar Palsapure Avatar answered Oct 03 '22 22:10

Amar Palsapure