473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

split()

Hi friends,

i need some urgent help from u.

i would like to divide a string using a "\"(pathseparator) as delimeter with Split method.

String str="c:\documents\csr\text.txt

i need the last word text.txt.

String str1 =str.split('\')[];

i used like this ,but i am not getting the output,please help me.i tried it in google and in some other forums, but i didnt get.

and another one, is there any way to get last word directly without looping the array?

please as early as possible

Thanks in advance
Jun 19 '07 #1
2 1158
Plater
7,872 Expert 4TB
You would need to use:
Expand|Select|Wrap|Line Numbers
  1. //Note: The @ just means you don't want to escape any characters like \
  2. string str=@"c:\documents\csr\text.txt";
  3. string[] pieces = str.Split("/".ToCharArray());
  4. string myending=pieces[pieces.Length - 1];
  5. \\myending should now contain "text.txt"
  6.  
Although, if you are only dealing with paths/files. Using System.IO.File and System.IO.FileInfo (and counter parts for Directory/DirectoryInfo) might also be used to do what you want.
Jun 19 '07 #2
You can use one of these: (Code is in C#)
1.
Expand|Select|Wrap|Line Numbers
  1. sResult = @"C:\Temp\myfile.txt";
  2. int index = sResult.LastIndexOf('\\');
  3. sResult = sResult.Substring(index + 1);
  4.  
2. Or, if you are using this for file or dir path, you can import System.IO namespace. Then u can use static functions from Path or File or Directory classes to break your path into directory and file name (with or without extn etc.)
Paste this code and call it from a windows form
Expand|Select|Wrap|Line Numbers
  1. public string PlayPathFleNameEtc(string sPath)
  2.         {
  3.             textBox1.Text = "*** GetDirectoryName: " + Path.GetDirectoryName(sPath);
  4.             textBox1.Text += Environment.NewLine + "*** GetExtension: " + Path.GetExtension(sPath);
  5.             textBox1.Text += Environment.NewLine + "*** GetFileName: " + Path.GetFileName(sPath);
  6.             textBox1.Text += Environment.NewLine + "*** GetFileNameWithoutExtension: " + Path.GetFileNameWithoutExtension(sPath);
  7.             textBox1.Text += Environment.NewLine + "*** GetFullPath: " + Path.GetFullPath(sPath);
  8.             textBox1.Text += Environment.NewLine + "*** GetPathRoot: " + Path.GetPathRoot(sPath);
  9.             textBox1.Text += Environment.NewLine + "*** GetRandomFileName: " + Path.GetRandomFileName();
  10.             textBox1.Text += Environment.NewLine + "*** GetTempFileName: " + Path.GetTempFileName();
  11.             textBox1.Text += Environment.NewLine + "*** GetTempPath: " + Path.GetTempPath();
  12.  
  13.             return "Success, check TextBox Contents.";
  14.         }
  15.  
Jun 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.