Cutting from a string in C #

My lines look like this: aaa / b / cc / dd / ee. I want to cut the first part without /. How should I do it? I have many lines, and they do not have the same length. I tried using Substring (), but what about /?

I want to add 'aaa' to the first treeNode, 'b' to the second, etc. I know how to add something to a treeview, but I do not know how I can get these parts.

+3
source share
5 answers

Perhaps the Split () method is what you are after?

string value = "aaa/b/cc/dd/ee";

string[] collection = value.Split('/');

Defines substrings in this instance that are limited to one or more characters specified in the array, and then places the substrings in an array of strings.

, TreeView (ASP.Net? WinForms?), :

foreach(string text in collection)
{
    TreeNode node = new TreeNode(text);
    myTreeView.Nodes.Add(node);
}
+5

Substring IndexOf, /

:

// from memory, need to test :)
string output = String.Substring(inputString, 0, inputString.IndexOf("/")); 

:

// from memory, need to test :)
string output = String.Substring(inputString, 
                                 inputString.IndexOf("/"),     
                                 inputString.Length - inputString.IndexOf("/"); 
+4

, , :

string[] parts = "aaa/b/cc/dd/ee".Split(new char[] { '/' });
+1

, ... !

0

- string.Split , string.Join , , .

:

var parts = input.Split('/');
var processedInput = string.Join("/", parts.Skip(1));

. , string.IndexOf, :

var processedInput = input.Substring(input.IndexOf('/') + 1);
0

Source: https://habr.com/ru/post/1781477/


All Articles