Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2005, 01:03 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to split a string

Hi
I have a string in this form "L/BNG/0153". I have to retrieve "0153" after splitting.Either with sql server or with C# if anyone can do,please give the reply.


Thanks
Lily

 
Old March 18th, 2005, 02:25 AM
Authorized User
 
Join Date: Jul 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aravwind Send a message via Yahoo to aravwind
Default

i tried in javascript.
 this is wrking fine.
try to implement the same in c#.

[u]Assumption</u>:-

assuming that the sample data that you have given is the
format for your input data. i.e.
assuming that after the last "/" only the number is coming.


<script language="javascript">
var arr;
var strvar;
strvar="L/BNG/0153";
arr = strvar.split("/");
alert(arr[arr.length-1]);
</script>
 
Old March 18th, 2005, 02:49 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.But what i want exactly is i have to retrieve the 3 part after slash(/).
U have hard coded but i have to pass the parameter through sql server and retrieve the number.
Can u help me in this regard.
I have written a stored procedure:

Create procedure ParseArray ( @Array varchar(1000), @separator char(1) ) AS

set nocount on
-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
declare @separator_position int -- This is used to locate each separator character
declare @array_value varchar(1000) -- this holds each array value as it is returned

-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
set @array = @array + @separator

-- Loop through the string searching for separtor characters
while patindex('%' + @separator + '%' , @array) <> 0
begin

  -- patindex matches the a pattern against a string
  select @separator_position = patindex('%' + @separator + '%' , @array)
  select @array_value = left(@array, @separator_position - 1)

  -- This is where you process the values passed.
  -- Replace this select statement with your processing
  -- @array_value holds the value of this element of the array
  select Array_Value = @array_value

  -- This replaces what we just processed with and empty string
  select @array = stuff(@array, 1, @separator_position, '')
end

set nocount off
GO

I am getting the result for (L/BNG/0153)as

Array_Value
----------
L

Array_Value
----------
BNG

Array_Value
----------
0153

But i dont know how to get them individually,this i am getting as an array.






 
Old March 18th, 2005, 04:42 AM
Authorized User
 
Join Date: Jul 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aravwind Send a message via Yahoo to aravwind
Default

Please clarify me.
i want to know what is stored in the database.

and what you want to display.

if say e.g., "L/BNG/0153" in database
and you want to display "0153"
this code will work if the required string ends with
this pattern say /1767674645.....


what you need to do is retrieve the value from the
database and assign in the string "strvar"
down below.

This is for c#
================
String arr;
String strvar;
strvar="L/BNG/0153";
arr = strvar.split("/");
system.console.write(arr[arr.length-1]);

==============================================

if you want to do the manipulations in SQL itself means

step1: reverse the string
step2: use PATINDEX and search for "/"
step3: store the "/" position in a variable
step4: now take substring(string,1,position obtained in step3)

this will do for you.
 
Old March 18th, 2005, 07:30 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In database this is the membership no. of members,its storing as L/BNG/01234 or SLM/HYD/01235/2004 like this. I have to retrieve the 3rd string after slash i.e 01234 or 01235 respectively.

 
Old March 18th, 2005, 01:16 PM
Authorized User
 
Join Date: Mar 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its very simple lily just use substring method
YourString.substring(startindex,no of characters uwant);
as string str="asdfghjk';
str.substring(1,5);








Similar Threads
Thread Thread Starter Forum Replies Last Post
how to split string krevathi1912 Access VBA 10 September 6th, 2007 02:21 AM
string split vidhya XSLT 1 August 10th, 2005 04:53 AM
string split by : crmpicco Classic ASP Basics 3 February 10th, 2005 07:49 AM
String.Split bbhill General .NET 2 April 10th, 2004 05:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.