Jump to content

C# remove last punctuation and everything after it on a string

Joveice

Title says it all, I want to remove the last punctuation and everything that comes after.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

string MyString = "Hello, Th,ere.";

if (MyString.Contains(",") == true)
{
	int MyLength = MyString.Length - MyString.LastIndexOf(',');
	LabelOut.Text = MyString.Remove(MyString.LastIndexOf(','),MyLength);
}

Result:
Hello, Th

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, MikeSK said:

string MyString = "Hello, Th,ere.";

if (MyString.Contains(",") == true)
{
	int MyLength = MyString.Length - MyString.LastIndexOf(',');
	LabelOut.Text = MyString.Remove(MyString.LastIndexOf(','),MyLength);
}

Result:
Hello, Th

Why not split the string on , then join all but the last array item? 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, vorticalbox said:

Why not split the string on , then join all but the last array item?

 

3 hours ago, Joveice said:

last punctuation and everything that comes after.

With a Split, if you want to keep all the commas with the exception of the last one and everything after it, when rejoin, you have to add the commas back in.

Post your solution.  Lets see what you come up with. 

Link to comment
Share on other sites

Link to post
Share on other sites

 

string myString = "Hello, Th,ere.";
var n = myString.LastIndexOf(',');
if(n != -1)
{
  myString = myString.Substring(0, n);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Dune II said:

string myString = "Hello, Th,ere."; 
var n = myString.LastIndexOf(','); 
if(n != -1) 
{ 
	myString = myString.Substring(0, n); 
}

 

In my implementation of C#,
I had to change the code to the following to work for me.
 

string MyString = "Hello, The,re.";

int? n = MyString.LastIndexOf(',');

if (n != -1)
{
	LabelOutput.Text = MyString.Substring(0,(int)n);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

No need to use a nullable int, just use the normal int.

 

I'm used to use implicit var, but that only works in visual C# 3.0 and higher.

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, Dune II said:

No need to use a nullable int, just use the normal int.

You are right.   Thanks.  The following worked:
 

string MyString = "Hello, The,re.";

int n = MyString.LastIndexOf(',');

if (n != -1)
{
	LabelOutput.Text = MyString.Substring(0,n);
}

 
 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×