473,386 Members | 1,720 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,386 software developers and data experts.

substring numeric value of a string

109 100+
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 4095
Kapps
16
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 Expert 2GB
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
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
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
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 Expert 2GB
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
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 8TB
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.RegularExpressions of course.
Oct 7 '08 #9
arial
109 100+
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
arial
109 100+
Hi all,

Back to my problem.

I will to save numeric value in separate string as well as the characters.

like,,,,,12345 abcdefh

I need string1 = "12345" and string2 = "abcdefh"

Any idea how can i do this?
Oct 13 '08 #11
Plater
7,872 Expert 4TB
Hi all,

Back to my problem.

I will to save numeric value in separate string as well as the characters.

like,,,,,12345 abcdefh

I need string1 = "12345" and string2 = "abcdefh"

Any idea how can i do this?
Expand|Select|Wrap|Line Numbers
  1. //asuming always divided by a space
  2. string startstring="12345 abcdefh";
  3. string[] pieces startstring.Split(' ');
  4. pieces[0]="12345";//can be parsed by int.Parse() now
  5. pieces[1]= "abcdefh";
  6. //note there could be a pieces[3]and etc if there are more spaces
  7.  
Oct 13 '08 #12
r035198x
13,262 8TB
Hi all,

Back to my problem.

I will to save numeric value in separate string as well as the characters.

like,,,,,12345 abcdefh

I need string1 = "12345" and string2 = "abcdefh"

Any idea how can i do this?
Did you read/try my post above? Regex is the proper way of doing these things.
Oct 14 '08 #13
arial
109 100+
Thank you all for help. My problem is solved.

Thank you,
Oct 14 '08 #14

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

Similar topics

7
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...
11
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...
2
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
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">...
6
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 =...
4
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...
11
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...
4
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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...

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.