Solved

string separate in two variables asp.net c#

Posted on 2016-09-27
5
51 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 (Megan)
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 (Megan) 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 (Megan)
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
New to SOAP and need some direction 10 40
Interview questions for support of a legacy ASP.NET site 4 37
Gridview alignment 1 0
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

14 Experts available now in Live!

Get 1:1 Help Now