473,809 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

substring numeric value of a string

109 New Member
Hi all,

I need some help from you expert. I need to extract numeric value of a string.

for ext: string str = "1234 absc st" or it can be "123 hello world"
or "1234567 asp.net"


How can i only get the numeric part of the string?

Please help and give me some suggestions.

Thank you,
Oct 6 '08 #1
13 4141
Kapps
16 New Member
One potential way would be to go through each char in the string, and check if it's numeric. If it is, add it on to a temporary string, which is then returned. I'm not sure how efficient this is however.
Oct 7 '08 #2
Curtis Rutland
3,256 Recognized Expert Specialist
Regular expressions would probably be useful for this.

I'm not good with RegEx myself, so I can't give you a sample, but maybe someone else can help with that.
Oct 7 '08 #3
chelvan
90 New Member
hi

you always find a space after your numeric value on your string. so its simple, first you find the position of the space , then you able to copy your numeric value by using substring.

regards
chel-1
Oct 7 '08 #4
artov
40 New Member
If the number is at the first, you might like to use Split method using space
as separator, the Parse (or better yet, TryParse) to get the value.
Oct 7 '08 #5
chelvan
90 New Member
split your string into an string array by space position. then convert your array element into integer. exception will throws when you cast the string. only one array element never throws exception thats element holds your answer.

chel-1
Oct 7 '08 #6
tlhintoq
3,525 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. string MyAddress = "123 N. Main Street";
  2. int HouseNumber = Convert.ToInt32(MyAddress.Substring(0, MyAddress.IndexOf(' ')));
  3. MessageBox.Show(HouseNumber.ToString(), "House number");
  4.  
Give this a try.
Its up to you to beef it up, give it some value qualification and error handling. But should point you in a workable direction.
Oct 7 '08 #7
chelvan
90 New Member
Expand|Select|Wrap|Line Numbers
  1. string MyAddress = "123 N. Main Street";
  2. int HouseNumber = Convert.ToInt32(MyAddress.Substring(0, MyAddress.IndexOf(' ')));
  3. MessageBox.Show(HouseNumber.ToString(), "House number");
  4.  
Give this a try.
Its up to you to beef it up, give it some value qualification and error handling. But should point you in a workable direction.

hi its right.
but my question is whats your idea
when the string value turn to like this "abc 14526 kio mk";

chel-1
Oct 7 '08 #8
r035198x
13,262 MVP
Have a look at

Expand|Select|Wrap|Line Numbers
  1. string s = "56ttt777";
  2. foreach(string strItem in Regex.Split(s,"\\D")){
  3.     Console.WriteLine(strItem);
  4. }
You'd need use System.Text.Reg ularExpressions of course.
Oct 7 '08 #9
arial
109 New Member
Thank you all for all your suggetion.

like chel-1 said i need to make sure it works both way,
123 abc and abnd 345 edf.

I will give a try to your suggetion and let you all know what happens.

Thank You,
Oct 7 '08 #10

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

Similar topics

7
14819
by: cpp_weenie | last post by:
Given a std::string of the form "default(N)" where N is an integer number of any length (e.g. the literal string might be "default(243)"), what is the quickest way to extract the characters representing the integer into another std::string? In the example above, I'd want to end up with a std:string whose value is "243". The substrings "default(" and ")" are invariant - they're always present in the string I have to work with. ...
11
5363
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
2
2028
by: David Filion | last post by:
Hi, I have a question about substring(), when I run the following query: prepaid=# select substring('15148300', 0, 5); substring ----------- 1514 (1 row)
4
3265
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic"> <xsl:param name="path" /> <fo:block padding-after="12pt">
6
9274
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will = "kelly" with no error // but string s2 = "kelly".Substring(0,200) // results in // ArgumentOutOfRangeException
4
1805
by: shapper | last post by:
Hello I have the following string: myName = "John Smith Curtis" I want to get the first word, i.e. "John", which means I want to get everything from the start to the first space. I know I should use Substring but I don't know what to do to remove the
11
7058
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the data when the first 8bits are 10101010,then it ll extract the following...for example: 10101010111000111 here is my code: Dim dataE As String Dim lot101, lot102, lot103, lot104 As String
4
24650
by: Serman D. | last post by:
I would like to extract the first 6 digits of a numeric value (e.g. the string '123456' out of the numeric 1234567890123456789). I tried a combination of CAST and SUBSTR, but it seems I am not doing the conversion properly. Please advice: $ cat substr.sql connect to viper @ create table largenum (id integer not null, lval decimal(21,0) not
3
2591
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I have coded using Substrings below? string s = TextBox1.Text.ToUpper(); string ch = s.Substring(0, 1); // read first character if ((ch == "B") || (ch == "C") || (ch == "X")) {
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10391
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.