Solved

string separate in two variables asp.net c#

Posted on 2016-09-27
5
60 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.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 43

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 43

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 43

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 13

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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