Share via

Remove string or char at begining.

MiPakTeh 1,476 Reputation points
2022-07-24T11:19:33.867+00:00

Hi All,

I have data in List Data4;

2 2484,7381,3099,6847,7716,7303,1255,1098,0070
332 1586,0764,1270,3897,9473,5890,5301,0690,6699
0142 5561,6611,5226,9148,3861,8905,9496,4827,8155

I want remove at front all lines so listbox show like bellow.

2484,7381,3099,6847,7716,7303,1255,1098,0070
1586,0764,1270,3897,9473,5890,5301,0690,6699
5561,6611,5226,9148,3861,8905,9496,4827,8155

Code I test;

       private void button3_Click(object sender, EventArgs e)  
        {  
  
            foreach (var st in Data4.Skip(0).Select(s => s.Split(new[] { ',' }, 3)[2]))  
            {  
                listBox5.Items.Add(st);  
            }  
  
        }  
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

P a u l 10,766 Reputation points
2022-07-24T11:29:42.567+00:00

As the delimiter between the first number and the remainder of the characters is a space, you could use String.IndexOf to get the first occurrence of that space, then use String.Substring to create a new string after that space:

   using System.Diagnostics;  
     
   var inputs = new[] {  
   	"2 2484,7381,3099,6847,7716,7303,1255,1098,0070",  
   	"332 1586,0764,1270,3897,9473,5890,5301,0690,6699",  
   	"0142 5561,6611,5226,9148,3861,8905,9496,4827,8155"  
   };  
     
   var expected = new[] {  
   	"2484,7381,3099,6847,7716,7303,1255,1098,0070",  
   	"1586,0764,1270,3897,9473,5890,5301,0690,6699",  
   	"5561,6611,5226,9148,3861,8905,9496,4827,8155"  
   };  
     
   var actual = new string[inputs.Length];  
     
     
   for (int i = 0; i < inputs.Length; i++) {  
   	var input = inputs[i];  
   	var spaceIndex = input.IndexOf(' ');  
     
   	if (spaceIndex != -1)  
   		actual[i] = input.Substring(spaceIndex + 1);  
   }  
     
   for (int i = 0; i < expected.Length; i++) {  
   	Debug.Assert(expected[i] == actual[i], "Expected and actual values do not match!");  
   }  

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-07-24T13:38:52.993+00:00

    How about a language extension, in this case with .NET Core

    public static class StringExtensions  
    {  
        /// <summary>  
        /// Trim from start a given string  
        /// </summary>  
        /// <param name="target">String to work on</param>  
        /// <param name="trimString">String to remove from start of <see cref="target"/></param>  
        /// <returns>Original string if not starting with <see cref="trimString"/> or <see cref="target"/> minus<see cref="trimString"/></returns>  
        public static string RemoveFromStart(this string target, string trimString)  
        {  
            if (string.IsNullOrWhiteSpace(trimString)) return target;  
      
            string result = target;  
            while (result.StartsWith(trimString))  
            {  
                result = result[trimString.Length..];  
            }  
      
            return result;  
        }  
    }  
    

    Usage

    var line =   
        "2 2484,7381,3099,6847,7716,7303,1255,1098,0070 332 " +   
        "1586,0764,1270,3897,9473,5890,5301,0690,6699 0142 5561," +   
        "6611,5226,9148,3861,8905,9496,4827,8155";  
      
    var result = line.RemoveFromStart("2 ");  
      
    

    If not .NET Core change result = result[trimString.Length..]; to result = result.Substring(trimString.Length);

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.