Solved

string separate in two variables asp.net c#

Posted on 2016-09-27
5
45 Views
Last Modified: 2016-09-27
We have a lot of string variables in an asp.net c# application with values like "document.doc"' "powernt,ppt", school.xls"', "theme.pdf" and son on. How can we separate in code behind in 2 new strings each variable and that 2 new variables must be: First new string is the name before the "." and the second string the extension or the name after the "."

 For example using one example I wrote:

main string : "document.doc" and this we want to store in 2 new variables like this:
first new variable value "document"
second new variable "doc"

In short is like separate the name and extension in 2 new variables.

What command or script can be used to do that in codebehind?
0
Comment
Question by:Alex E.
  • 3
5 Comments
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41818630
You can use the Split() method.

Here is a Fiddle Demo

using System;
					
public class Program
{
	public static void Main()
	{
		var myString = "document.doc";
		var mySplit = myString.Split('.');
		for(int i = 0; i < mySplit.Length; i++) 
		{
			Console.WriteLine(mySplit[i]);
		}
	}
}

Open in new window

0
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 250 total points
ID: 41818650
Another way to do this would be to use IO.Path methods, if the string is always a file name.

Here is a Fiddle Demo

using System;
using System.IO;
					
public class Program
{
	public static void Main()
	{
		var myString = "document.doc";
		Console.WriteLine(Path.GetFileNameWithoutExtension(myString));
		Console.WriteLine(Path.GetExtension(myString).Replace(".",""));
	}
}

Open in new window

0
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41818669
And as a side note, it may be useful to use a Class object instead of a variable

Fiddle Demo

using System;
using System.IO;
					
public class Program
{
	public static void Main()
	{
		MyThing thing = new MyThing("document.doc");
		Console.WriteLine(thing.FileName);
		Console.WriteLine(thing.Extension);
	}
	
	public class MyThing 
	{
		public string FullName { get; set; }
		public string FileName { get; set; }
		public string Extension { get; set; }
		
		public MyThing(string thing)
		{
			FullName = thing;
			FileName = Path.GetFileNameWithoutExtension(thing);
			Extension = Path.GetExtension(thing).Replace(".","");
		}
	}
}

Open in new window

0
 
LVL 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 250 total points
ID: 41818711
Another way is to use substring to split it up.  See this example:

            string document = "document9.27.2016.doc";
            int extensionMarker = document.LastIndexOf('.');
            string filename = document.Substring(0, extensionMarker);
            string extension = document.Substring(extensionMarker+1);

Open in new window


Be aware of the method you use though (I'd use this or IO.Path) so that if you filename contains multiple periods (as mine does above) it always looks to the last one.
0
 

Author Closing Comment

by:Alex E.
ID: 41818743
Thank you worked perfect!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now