Delete parts of a string from one letter to another?

I’m working on a little code interpreter but I’m stuck removing comments, I’m trying to remove from // to the end of the line of a string?

So, I’m trying to remove parts of strings how I need to do it is go from “//” to the end of the line or “/n” But I’m finding it hard?

For example:

// Hello 1
Hello 2//Hello 3

but just print Hello 2?

Any help on how to do this, I know about string.split but I don’t think that is the right thing to use?

A regular expression will sort this for you.

var sourceString : String = "this is uncommented code // this is a code comment";
var regEx : String = "//"; //<-is the comment start indicator
var results : String[] = Regex.Split(sourceString, regEx);

/*results at index 0 should contain all characters 
before first occurrence of the comment indicator */
Debug.Log(results[0]);

Unfortunately that doesn’t work because it does not take into account the next line. Thus will think the whole thing is just one line.
For example in my test situation it would print:

Hello 1Hello 2

and:

Hello 3

You would need to process line by line.

I had a little method somewhere… ahh here it is

static string StripComments(string text)
{
    var regexTest = @"(@(?:""[^""]*"")+|""(?:[^""\n\\]+|\\.)*""|'(?:[^'\n\\]+|\\.)*')|//.*|/\*(?s:.*?)\*/";
    return Regex.Replace(text, regexTest, "$1");
}

How are you getting the data that needs to be stripped?

Thanks for the help guys, I got this working now :):

using UnityEngine;
using System.Collections;
using System;

public class RemoveComments : MonoBehaviour 
{
    public TextAsset asset;//The orginal program including comments.
	string[] Lines;//This is the array of the lines.
	
	string[] CMT = new string[] {"//"};// This is an array of comments for me this is just //
	string[] NWL = new string[] {"\n"};// This is the array of places to split, for me this is a new line(\n)
	
	string Program;
    void Start() 
	{   
		Lines = asset.text.Split(NWL,StringSplitOptions.None);// First split it into lines.
		foreach(string Line in Lines)//For each line in the asset
		{
		 string[] Comments = Line.Split(CMT, StringSplitOptions.None);//Split lines at //
		 Program += Comments[0];//Comments[0] is before the // and comments [1] is after, so we add the non comments to the program.
		}
		Debug.Log (Program);
	}
}