Zahl aus einem string filtern

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Horst der Biber

Betreff: Zahl aus einem string filtern

BeitragMi, Mai 24, 2006 22:14
Antworten mit Zitat
Benutzer-Profile anzeigen
hab so was ma für ne konsole gebraucht. aber keinen befehl gefunden der das kann..

Code: [AUSKLAPPEN]
Function zahlausstringfiltern:int(text:string)
Local zahl:string

For i=1 To Len(text)
If Mid(text,i,1)="0" Or Mid(text,i,1)="1" Or Mid(text,i,1)="2" Or Mid(text,i,1)="3" Or Mid(text,i,1)="4" Or Mid(text,i,1)="5" Or Mid(text,i,1)="6" Or Mid(text,i,1)="7" Or Mid(text,i,1)="8" Or Mid(text,i,1)="9" Then
zahl:+Mid(text,i,1)
EndIf
Next
return int(zahl)
End Function


wär nett, wenn wer bugs oder ergänzungen posten würde..
  • Zuletzt bearbeitet von Horst der Biber am Do, Mai 25, 2006 10:51, insgesamt einmal bearbeitet

Markus2

BeitragMi, Mai 24, 2006 22:32
Antworten mit Zitat
Benutzer-Profile anzeigen
Unter Help/Language/Strings gibt es sowas .

Strings
Strings are used to store sequences of characters.

Strings are actually objects, so can be used where ever an object is expected. Strings provide the following methods:

Method Description
Find:Int( subString:String,startIndex=0 ) Finds first occurance of a sub string. Returns -1 if subString not found.
FindLast:Int( subString:String,startIndex=0 ) Finds last occurance of a sub string. Returns -1 if subString not found.
Trim:String() Removes leading and trailing non-printable characters from a string.
Replace:String( subString:String,withString:String ) Replaces all occurances of subString with withString.
StartsWith:Int( subString:String ) Returns true if a string starts with subString.
EndsWith:Int( subString:String ) Returns true if a string ends with subString.
Contains:Int( subString:String ) Returns true if a string contains subString.
ToLower:String() Converts a string to lowercase.
ToUpper:String() Converts a string to uppercase.
ToInt:Int() Converts a string to an integer.
ToLong:Long() Converts a string to a long.
ToFloat:Float() Converts a string to a float.
ToDouble:Double() Converts a string to a double.
ToCString:Byte Ptr() Converts a string to a null terminated sequence of 8 bit bytes. The returned memory must be freed with a call to MemFree.
ToWString:Short Ptr() Converts a string to a null terminated sequence of 16 bit shorts. The returned memory must be free with a call to MemFree.
...

Horst der Biber

BeitragMi, Mai 24, 2006 22:49
Antworten mit Zitat
Benutzer-Profile anzeigen
seh grad dass wenn man .toint() benutzt und der string zb so ausschaut "bla123bla" 0 zurückgegeben wird.. un bei meiner halt 123..

Markus2

BeitragMi, Mai 24, 2006 23:01
Antworten mit Zitat
Benutzer-Profile anzeigen
Na gut ,
dann könntest du im oberen Teil wenigstens Select Case benutzen
und nach dem String zusammen basteln ToInt aufrufen Wink

Jolinah

BeitragDo, Mai 25, 2006 0:03
Antworten mit Zitat
Benutzer-Profile anzeigen
Nur zur Info, in BMax kann man mit jedem beliebigen Objekt casten (umwandeln), nach dem Prinzip:

TypeName(Wert)

Das heisst ein String kann auch in ein Int verwandelt werden mit:

Code: [AUSKLAPPEN]
Local s:String = "1278"
Local i:Int = Int(s)

Das funktioniert aber nach dem selben Prinzip wie toint(), wenn der String mit Buchstaben beginnt, kann keine Zahl daraus gewonnen werden.

Wie gesagt geht das mit jedem Type bzw. Objekt. Wenn das Casten (Umwandeln) nicht möglich ist bekommt man Null bzw. 0 zurück. Mit Ifs kann man prüfen von welchem Type ein Objekt ist.

Code: [AUSKLAPPEN]
Type bla
  Field a
End Type

Type test
  Field b
End Type

Local b:bla = new bla
Local t:test = new test
Local o:object = b

If bla(o)
  Print "o ist vom type bla"
Elseif test(o)
  Print "o ist vom type test"
Endif

b = bla(o) 'Objekt in Type bla umwandeln

Horst der Biber

BeitragDo, Mai 25, 2006 10:54
Antworten mit Zitat
Benutzer-Profile anzeigen
das mit dem casten wusst ich gar ned.. hab den ersten post ma editiert.

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group