Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string separate in two variables asp.net c#

Posted on 2016-09-27
5
Medium Priority
?
80 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 44

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 44

Accepted Solution

by:
zephyr_hex (Megan) earned 1000 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 44

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 14

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

972 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